Suggestions for good programming practices?

Bengt Richter bokr at oz.net
Tue Jun 25 15:32:12 EDT 2002


On Mon, 24 Jun 2002 18:55:22 -0700, "David LeBlanc" <whisper at oz.net> wrote:

>They may be returned in a tuple, but they're PUT in discrete variables and
ITYM bound to discrete variable names

>not kept in a tuple that I have to programmatically unpack.
>
Right, it's programmatically done for you ;-)

 >>> def foo(): return (1,2,3)
 ...
 >>> a,b,c = foo()
 >>> a
 1
 >>> b
 2
 >>> c
 3
 >>> import dis
 >>> dis.dis(foo)
           0 SET_LINENO               1

           3 SET_LINENO               1
           6 LOAD_CONST               1 (1)
           9 LOAD_CONST               2 (2)
          12 LOAD_CONST               3 (3)
          15 BUILD_TUPLE              3
          18 RETURN_VALUE
          19 LOAD_CONST               0 (None)
          22 RETURN_VALUE

 >>> from miscutil import disex
 >>> disex('a,b,c = foo()')
           0 SET_LINENO               0

           3 SET_LINENO               1
           6 LOAD_NAME                0 (foo)
           9 CALL_FUNCTION            0
          12 UNPACK_SEQUENCE          3
          15 STORE_NAME               1 (a)
          18 STORE_NAME               2 (b)
          21 STORE_NAME               3 (c)
          24 LOAD_CONST               0 (None)
          27 RETURN_VALUE

Note the "UNPACK_SEQUENCE" and "STORE_NAME"s.

>Language lawyer should be shot, _then_ heard.
Along with top-posters? ;-)

>
>David LeBlanc
>Seattle, WA USA
>
>> -----Original Message-----
>> From: python-list-admin at python.org
>> [mailto:python-list-admin at python.org]On Behalf Of Peter Hansen
>> Sent: Monday, June 24, 2002 18:03
>> To: python-list at python.org
>> Subject: Re: Suggestions for good programming practices?
>>
>>
>> David LeBlanc wrote:
>> >
>> > The one that always gets me is "lat,lon,timezone =
>> getLocation("seattle")".
>> > I.E. the ability to return multiple distinct values as opposed
>> to returning
>> > multiple values in the form of a struct as in C or C++.
>>
>> You _are_ returning them "in a struct".  It's spelled "tuple"
>> but other than that what's the difference?
>>
>> -Peter
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
>

Regards,
Bengt Richter



More information about the Python-list mailing list