def a((b,c,d),e):

John Machin sjmachin at lexicon.net
Mon Apr 18 18:41:06 EDT 2005


On 18 Apr 2005 13:05:57 -0700, "AdSR" <artur_spruce at yahoo.com> wrote:

>Fellow Pythonistas,
>
>Please check out
>
>http://spyced.blogspot.com/2005/04/how-well-do-you-know-python-part-3.html
>
>if you haven't done so yet. It appears that you can specify a function
>explicitly to take n-tuples as arguments. It actually works, checked
>this myself. If you read the reference manual at
>http://docs.python.org/ref/function.html
>really carefully, you will find that it is indeed part of the language
>spec, but it's a likely candidate for the least advertised Python
>feature. Small wonder since it looks like one of those language
>features that make committing atrocities an order of magnitude easier.
>

Thanks for pointing this out. However I see no atrocity potential here
-- what did you have in mind?

See below. Better documentation in the "def" (even better than having
say "year_month_day" instead of my lazy "dt_tup"). No overhead;
byte-code is the same.

>>> def weird_date_from_tuple(dt_tup):
...    year, month, day = dt_tup
...    return
...
>>> import dis
>>> dis.dis(weird_date_from_tuple)
  2           0 LOAD_FAST                0 (dt_tup)
              3 UNPACK_SEQUENCE          3
              6 STORE_FAST               3 (year)
              9 STORE_FAST               1 (month)
             12 STORE_FAST               2 (day)

  3          15 LOAD_CONST               0 (None)
             18 RETURN_VALUE
>>> def weird_date_from_tuple((year, month, day)):
...    return
...
>>> dis.dis(weird_date_from_tuple)
  1           0 LOAD_FAST                0 (.0)
              3 UNPACK_SEQUENCE          3
              6 STORE_FAST               1 (year)
              9 STORE_FAST               2 (month)
             12 STORE_FAST               3 (day)

  2          15 LOAD_CONST               0 (None)
             18 RETURN_VALUE
>>>

Cheers,
John





More information about the Python-list mailing list