Python Forum

Full Version: Being explicit about the items inside a tuple argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, this might be a strange question but I was wondering if I can give a hint about
the tuple argument items. For isntance, in the example bellow:

# I don't want 2 arguments
def matchObjects(source=None, target=None):
    pass

# I want one argument like this
def matchObjects(objects=(None, None)):
    pass
The thing that I don't like in the tuple argument "objects=(None, None)" is that unless you read the docstring
you don't know which item is the source and which one is the target.
Is it possible to make that tuple more explicit about what it represents?

I am using python 2.7.

thanks
R
You could use a collections.namedtuple
>>> from collections import namedtuple
>>> Pair = namedtuple('Pair', 'source target')
>>>
>>> def matchObjects(pair=Pair(source=None, target=None)):
...     print(pair)
... 
thanks, that might be an option, I'll think about it.
As you don't enforce keyword arguments anyone can call function without keyword arguments (and without arguments at all) and not be any wiser. Maybe you should use *,?

>>> matchObjects(None, None):      # user: hmmm.... is source first or second
>>> matchObjects()                 # ursr: hmmm.... what is this function doing?