Python Forum

Full Version: Weird function defaults error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I'm making a blockchain module, and made a function that adds a new block object to a given list. I don't understand anything here so can't give much context.

[Image: Txb8Chg.png]
Exactly what it suggests: parameters with default values need to come after those without defaults (so in this case, wallet and data need to come first).
Python functions can have two types of arguments; position and key value. These correspond to the non-default and default terms used in the error you are seeing.

A key value argument is an argument that has a default value. In your function vhain, trans, receiver and amount are all key value arguments. Position arguments do not have a default value. In your function wallet and data are position arguments.

The rule in python is that all position arguments must appear first in the argument list, followed by all the position arguments. vhain is before wallet and data, a default argument before a non-default argument. You will have to move vhain.
Ahhhh. I don't understand why Python would be designed that way, but thanks anyway!
I suppose if keyword arguments were allowed before positional ones, there could be ambiguity. Consider

def foo(bar=1, baz): pass

If you write foo("qux"), what does that mean? Would it be passing a value for baz (using the default for bar), or passing a value for bar?

(I might not be thinking this through entirely, but that was my first thought)
Python does it this way because when you call a function you can treat key value arguments as position arguments.
def mixedbag(pos1, pos2, keyvalue1=1, keyvalue2=2):
    pass

mixedbag(1, 2, 3, 4) # This is allowed.  pos1=1, pos2=2, keyvalue1=3, keyvalue2=4
If you ere allowed to intermix position and key value arguments the mapping of these values would become confused:
def mixedbag(pos1, keyvalue1=1, pos2, keyvalue2=2):
    pass

mixedbag(keyvalue2 = 1, 2, 3) # Who gets 1?