Python Forum
Being explicit about the items inside a tuple argument
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
Reply
#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)
... 
Reply
#3
thanks, that might be an option, I'll think about it.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  search in dict inside tuple steg 1 644 Mar-29-2023, 01:15 PM
Last Post: rob101
  Calculate the sum of the differences inside tuple PUP280 4 1,149 Aug-12-2022, 07:20 PM
Last Post: deanhystad
  How to check if my argument is a tuple with 3 tuples zarox 1 1,794 Nov-15-2020, 06:50 PM
Last Post: DougBTX
  How to use a tuple as an argument of a function zarox 5 3,460 Nov-14-2020, 08:02 PM
Last Post: buran
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,725 Nov-04-2020, 11:26 AM
Last Post: Aggam
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,728 Mar-03-2020, 08:34 AM
Last Post: buran
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,115 May-21-2019, 11:39 AM
Last Post: avorane
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 3,161 May-11-2019, 03:18 AM
Last Post: keames
  Iterate a tuple with dict inside anna 2 2,374 Feb-13-2019, 03:44 PM
Last Post: anna
  convert non-string with explicit base jacklee26 5 16,056 Nov-06-2018, 06:50 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020