Skip to content
Advertisement

Default parameter positioning and TypeError

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 get TypeError: SubElement() argument 1 must be xml.etree.ElementTree.Element, not None What I need is to pass the value 20 (FLASHCMD_TIMEOUT) to the function op_sndrcv(). create_command() op_sndrcv() are used my multiple routines and I want to keep this as optional, without changing the current format, but just by introducing the new variable timeout

But when I modify the function like below, it doesn’t throw an error, but I end up using wrong value (50 instead of 20)

def op_sndrcv(self, command, log=True, resp_needed=False, timeout = def.DEFAULT_TIMEOUT):
       # Do process the command and return response
       response = self.com.sndCmd(command, timeout, resp_needed)

Any help would be highly appreciated!

Advertisement

Answer

It looks like your issue is that you’ve changed the order of the arguments to op_sndrcv without modifying the call in createcommand to respect the change. There are two possibilities to fix this. Either modify op_sndrcv so the order of parameters is the same as before:

def op_sndrcv(self, command, timeout = def.DEFAULT_TIMEOUT, log=True, resp_needed=False)

or you can name the parameter you’re sending in createcommand:

response = self.op_sndrcv(my_cmd, timeout=timeout)

I suspect, however, that this is masking your actual error, which is that your first timeout was too short and is not giving your communication enough time to return before you go on to your next operation, which is causing the TypeError. I can’t confirm this with the code you’ve posted, though.

1 People found this is helpful
Advertisement