default-arguments

Default parameter positioning and TypeError

Default parameter positioning and TypeError Question: So here’s my code: FLASHCMD_TIMEOUT = 20 DEFAULT_TIMEOUT = 50 def createcommand(self): my_cmd = def.FLASHCMD timeout = def.FLASHCMD_TIMEOUT print(f"sending timeout val = {timeout}") response = self.op_sndrcv(my_cmd, timeout) return response def op_sndrcv(self, command, timeout = def.DEFAULT_TIMEOUT, log=True, resp_needed=False): # Do process the command and return response When I execute, I …

Total answers: 1

lambda *args, **kwargs: None

lambda *args, **kwargs: None Question: consider: blank_fn = lambda *args, **kwargs: None def callback(x, y, z=”): print x, y, z def perform_task(callback=blank_fn): print ‘doing stuff’ callback(‘x’, ‘y’, z=’z’ ) The motivation for doing it this way is I don’t have to put in logic to check if callback has been assigned because it defaults to …

Total answers: 2

How to pass a default argument value of an instance member to a method?

How can I use an attribute of the instance as a default argument for a method? Question: I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the …

Total answers: 6