Default function argument [Python]


#1
def kill_by_priority(e_types=None, melee_types_to_ignore=None, radius=9999):
    RANGE_TYPES = ['shaman', 'thrower', 'fangrider', 'archer']
    DEFAULT_PRIORITY = RANGE_TYPES + ['ogre', 'scout', 'munchkin']
    self.say(e_types)
    e_types = e_types or DEFAULT_PRIORITY
    self.say(e_types)

The first call of say throws an exception “Say what?” like for non-declared variable.

Without the say calls python “or trick” just doesn’t work - default value is always returned by the expression even if truthy argument was passed.

I tried explicit if and removing =None after argument name. That doesn’t work.

Has anyone a workaround for the case?


#2

I think you have to define a new variable that is equivalent to the input, e.g eTypes = e_types.


#3

eTypes is not pythonic, used arg_e_types. :smile:

Actually, thanks. That did the trick.