Any way to loop through object variables?
Dave Challis
dsc at ecs.soton.ac.uk
Thu May 29 06:57:47 EDT 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ah thanks, vars(...) was exactly what I was after. I'd come across
dir() before, but this returns more than I need.
Thanks again,
Dave
Saju Pillai wrote:
>
> On 28-May-08, at 9:49 PM, Gary Herron wrote:
>
>> Dave Challis wrote:
>>> Hi,
>>> Just wondering if there's a way to iterate through all variables which
>>> an object has set?
>>>
>>> Specifically, I'm using the OptionParser module, which returns an
>>> options object, containing all command line options as object variables.
>>> I'd like to iterate over these rather than getting at them by name.
>>>
>>> Cheers,
>>> Dave
>>
>> For this object (and many others), you can get at the attributes with
>> the vars(...) builtin.
>> It returns a dictionary of attribute names and values.
>
>
> Also dir(object) will get you the list of attributes of that object.
>
> -srp
>
>>
>>
>> Gary Herron
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> --
> http://mail.python.org/mailman/listinfo/python-list
- --
~~~~~~~~~~~~~~~~~o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.
<>< (
) Dave Challis ><> ) )
_(__dsc at ecs.soton.ac.uk__________________________________(____(___________
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFIPowrv26GZvAVVFERApQfAJ96bFa1wl1sbnDCNmbtf5/tpNqTaQCdHKwf
k8+Uv8yXn2Bh5ri6sf5qMmA=
=cnfO
-----END PGP SIGNATURE-----
More information about the Python-list
mailing list