473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

looping over a class attribute

Hello,
I would like to be able to make a loop over a given class
attributes. For example, with the following class definition :
class x: .... val1=1
.... val2=2
.... print x.val1 1 print x.val2 2

I would like to be able to make a loop like this dir(x) ['__doc__', '__module__', 'val1', 'val2'] for i in dir(x) : print x.i


and get an output like typing

print x.__doc__
print x__module__
print x.var1
print x.var2

instead of

Traceback (most recent call last):
File "<stdin>", line 2, in ?
AttributeError: class x has no attribute 'i'

Is there a way to do this? Thanks in advance from a clueless newbie to
the wonderful world of Python and OO programming.

Sebastien.
bi*************@ouranos.ca
Jul 18 '05 #1
2 1396
In article <b8*************************@posting.google.com> ,
biner <bi*************@ouranos.ca> wrote:

I would like to be able to make a loop like this
dir(x)['__doc__', '__module__', 'val1', 'val2'] for i in dir(x) : print x.i


print getattr(x, i)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #2
In article <b8*************************@posting.google.com> , biner wrote:
I would like to be able to make a loop like this
dir(x) ['__doc__', '__module__', 'val1', 'val2'] for i in dir(x) : print x.i


Like Aahz said, getattr(x, i) is your ticket. However, keep in mind that the
behavior of dir() is not guaranteed across current and future versions of
Python. You may have better luck using x.__dict__.keys() - or in Python 2.3,
you can get away with this:

for i in x.__dict__: print getattr(x, i)

Since dictionaries are their own key-iterators now.

--
..:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
: please talk to your son or daughter about parametric polymorphism. :
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
3
by: Cheryl | last post by:
I need a sample that shows how can loop through the attributes and display the data.(.net / c#) <?xml version="1.0" encoding="UTF-8"?> <riders> <rider> <prename id=>Lance</prename>...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
4
by: glenn | last post by:
Is there a way to loop through a classes fields without first knowing what fields are there? Trying to write a generic class that could build a query based on a classes fields and types. This...
4
by: jimmygoogle | last post by:
i have an xml file with links to websites but my output only prints the 1st website from each tag - any ideas why? i have an an xml file that looks sort of like this: <?xml version="1.0"...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
1
by: Robocop | last post by:
Having just started using C again after some years off, i've been stumped by a problem i think someone more experienced could probably solve pretty easily. I have these 4 objects (vectors), and i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.