Aug-07-2020, 03:00 PM
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]](https://i.imgur.com/Txb8Chg.png)
Weird function defaults error?
|
Aug-07-2020, 03:00 PM
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.
![]()
Aug-07-2020, 03:07 PM
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).
Aug-07-2020, 03:10 PM
(This post was last modified: Aug-07-2020, 03:24 PM by deanhystad.)
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.
Aug-07-2020, 03:13 PM
Ahhhh. I don't understand why Python would be designed that way, but thanks anyway!
Aug-07-2020, 03:19 PM
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)
Aug-07-2020, 05:55 PM
(This post was last modified: Aug-07-2020, 05:55 PM by deanhystad.)
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=4If 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? |
|