I have am writing a Python function that takes a timeout value as a parameter. Normally, the user will always use the same timeout value, but on occasion he may want to wait slightly longer. The timeout value is stored as a class instance variable. I want to use the class’ timeout instance variable as the default parameter. Currently, I am implementing this as follows:
def _writeAndWait (self, string, timeout = -1):
if (timeout == -1):
timeout = self._timeout
I was just wondering, is the proper way to use an instance variable as a default parameter? Or is there a better way that would avoid the “if” check?
The short answer to your question is no, you cannot eliminate the if statement, because Python examines the signature only once, and so the default is shared across all calls. Consider, for example:
Versus:
These two functions, though they look identical, actually have completely different behavior; in the first one, the default value of L is shared between invocations so that
addit(1)followed byaddit(2)will return, in the first case, two objects that are actually point to the same underlying object and that have a value of[1,2], while it the second case it will return two separate objects,[1]and[2].The only recommendation I have for you, then, is to use a default of
Nonesimply because that is the standard convention for parameters that have not been specified, but to continue using an if check, because using as a default value some existing object will have subtly different (usually wrong) behavior.