Python Class iteration howto !
Aahz Maruch
aahz at panix.com
Sat Sep 9 12:21:52 EDT 2000
More information about the Python-list mailing list
Sat Sep 9 12:21:52 EDT 2000
- Previous message (by thread): Python Class iteration howto !
- Next message (by thread): default values of function parameters (construtors)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In article <8pd1k7$6t9$1 at nnrp1.deja.com>, Sindh <skodela at my-deja.com> wrote: > >Suppose I have a class xyz with attributed name,age,sex,address etc. How >do I iterate the values. > >eg: > >class xyz: > name='' > age=0 > sex='U' > address='U' > >def iter(a): > for n in dir(a): > print a.n I suggest that you reconsider your problem a bit. What happens if you create a class attribute that you don't want to print? Now, if you're talking about doing this in a debugging context, one of the other answers you've received is probably the correct approach. But if this is supposed to serve one of the code's working purposes, you're probably doing this the wrong way. I suggest one of two options: * create a print method in your class (which, unfortunately, cannot be called "print") that explicitly lists the attributes to be printed * put all of these printable attributes on a dictionary inside the class, which can then use the standard iteration techniques for dicts. -- --- Aahz (Copyright 2000 by aahz at pobox.com) Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/ Hugs and backrubs -- I break Rule 6 Usenet is not a democracy. It is a weird cross between an anarchy and a dictatorship. --Aahz
- Previous message (by thread): Python Class iteration howto !
- Next message (by thread): default values of function parameters (construtors)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list