Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Being explicit about the items inside a tuple argument
#1
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
Quote
#2
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)
... 
Quote
#3
thanks, that might be an option, I'll think about it.
Quote
#4
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 376 May-21-2019, 11:39 AM
Last Post: avorane
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 423 May-11-2019, 03:18 AM
Last Post: keames
  Iterate a tuple with dict inside anna 2 395 Feb-13-2019, 03:44 PM
Last Post: anna
  convert non-string with explicit base jacklee26 5 2,803 Nov-06-2018, 06:50 AM
Last Post: jacklee26
  Syntactic sugar for explicit lists? Antipaladin 6 945 May-11-2018, 06:22 AM
Last Post: volcano63
  Why args type is always tuple, when passed it as argument to the function. praveena 5 1,297 Jan-16-2018, 09:07 AM
Last Post: praveena
  Newb: Simple Explicit Formula Duplicitous 1 1,160 May-05-2017, 07:03 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)