2

when I try the following, I get error in the function definition itself.

>>> def mymap(*seq,pad=None):
  File "<stdin>", line 1
    def mymap(*seq,pad=None):
   SyntaxError: invalid syntax 

I am trying to give default value for the parameter pad.

but then, I tried this and it works (for wrong reason):

>>> def mymap(pad=None,*seq):
...    print seq
>>> mymap([1,2,3],[4,5,6])
([4, 5, 6],)
[(4,)]
>>> 

It is not printing the tuple of seq which should be ([1,2,3],[4,5,6]).

0

2 Answers 2

6

What you really want here is for pad to be a keyword-only parameter.

One of the major changes in 3.0 was designing function parameters and arguments to, among other things, provide exactly the feature you're looking for. In fact, your first version works exactly as you'd hope it to. But if you want to stick with 2.x, you don't get any of the new features in 3.x.

So, you have no choice but to fake it:

def mymap(*seq, **kwargs):
    pad = kwargs.pop('pad', None)
    if kwargs:
        raise TypeError("mymap() got an unexpected keyword argument '{}'".format(kwargs.keys()[0]))
    print seq

This does very nearly the exact same thing as the equivalent Python 3 code. Of course it's more complicated, a lot less clear, slower, and opaque to introspection… all of which is exactly why keyword-only parameters were added in 3.0. But if you need it, and you can't use Python 3.x, this is what you have to do. (Just be glad you're not trying to write C-API functions.)


You seem to be mixing up two entirely independent things: a parameter having a default value (which I assume is what you mean by the term "default parameter", which doesn't really mean anything), and being keyword-only.

You've already got a parameter with a default value, in your second version. You can see that easily: call mymap(), and it succeeds, with pad getting its default value of None (and seq being empty).

But you want it to also be a keyword-only parameter, so it doesn't steal the first positional argument. And that's the part you can't do in 2.x.


If you want to know more, PEP 3012 explains the keyword-only feature, and Arguments and parameters attempts to gather all the relevant documentation links, and summarize how everything fits together.

Sign up to request clarification or add additional context in comments.

8 Comments

so this means I cannot have default parameter when I have varargs in python < 3.0?
@eagertoLearn: No, that's not what it means. Let me edit to try to explain.
ok, by default value, I mean if I dont specify any value for it in the function call, it gets a default one. so a function def of test(*seq,pad=None): I mean, I will collect the parameters as tuple(by *seq) and if the default value for pad is set to be None
@eagertoLearn: Again, you're mixing up two things. test(pad=None, *seq) already has a default value—if you don't specify a value, it gets a default one. That's exactly what you asked for. The problem is that pad can get its value from a keyword argument or a positional argument or the default; you want it to only get its value from a keyword argument or the default, never from a positional argument. That's exactly the definition of a keyword-only parameter. Which you can't have in Python 2.
I am talking about test(*seq, pad=None) and not test(pad=None,*seq). In the first case, there is no default value (correct?) and the second case there is one..but I thought, in the first case there is a default value for pad. the problem with second case is when you call test(None,[1,2,3],[4,5,6]), you need to have None there. If I dont pass None, it gets the [1,2,3] for pad.
|
1

It looks like you are inputting the args [1,2,3] and [4,5,6] as two seperate arguments. They are not in a tuple.

mymap([1,2,3], [4,5,6]) << in this example, from your code, the [1,2,3] is being passed in for pad and the [4,5,6] is being passed in for seq. That's why printing seq results in [4,5,6]

Also, named arguments like pad, must come before *args or **kwargs.

For example:

mymap(None, [1,2,3],[4,5,6]) #passing None for pad

prints:

([1,2,3], [4,5,6])

1 Comment

Thats correct, by having default parameter, I dont want to give anything in the function call, that is the purpose of default parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.