Returning a tuple-struct
Tim Roberts
timr at probo.com
Sun Jan 22 01:52:01 EST 2006
groups.20.thebriguy at spamgourmet.com wrote:
>I've noticed that there's a few functions that return what appears to
>be a tuple, but that also has attributes for each item in the tuple.
>For example, time.localtime() returns a time.time_struct, which looks
>like a tuple but also like a struct. That is, I can do:
>
>>>> time.localtime()
>(2006, 1, 18, 21, 15, 11, 2, 18, 0)
>>>> time.localtime()[3]
>21
>>>> time.localtime().tm_hour
>21
Ah, but it ISN'T really a tuple:
>>> import time
>>> t = time.localtime()
>>> type(t)
<type 'time.struct_time'>
>>> str(t)
'(2006, 1, 21, 22, 49, 32, 5, 21, 0)'
It's a class object. The __repr__ method returns a string that LOOKS the
same as a tuple.
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list