Python Forum
Weird function defaults error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird function defaults error?
#1
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]
Reply
#2
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).
Reply
#3
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.
Reply
#4
Ahhhh. I don't understand why Python would be designed that way, but thanks anyway!
Reply
#5
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)
Reply
#6
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,363 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  Weird SQLAchemy connection string error pawpaw 0 1,493 Jun-28-2020, 10:11 AM
Last Post: pawpaw
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,097 Jul-22-2019, 06:14 AM
Last Post: buran
  Weird scoping error Stef 3 2,840 Jan-20-2019, 04:36 PM
Last Post: Stef
  Weird error in pycharm TheRedFedora 1 2,629 Mar-11-2018, 09:01 PM
Last Post: Larz60+
  weird error in random sentence generator bobger 9 5,575 Nov-29-2017, 07:34 PM
Last Post: bobger
  Error Handling is weird PythonAndArduino 1 2,962 Nov-09-2017, 05:08 AM
Last Post: Mekire
  Python beginner: Weird Syntax Error mentoly 5 10,253 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk
  Error in using the output of one function in another function (beginner) MadsPJ 6 4,978 Mar-13-2017, 03:06 PM
Last Post: MadsPJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020