Any way to loop through object variables?

Gary Herron gherron at islandtraining.com
Wed May 28 12:19:32 EDT 2008


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.

Gary Herron


--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list