473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterate over class variables

Hello list,

I need to iterate over a class and get all her variable names and
values, e.g. considering this example:
class testclass:
var1 = 'ab'
var2 = 'cd'
var3 = 'ef'

test = testclass()

Then I wanna do sonmething like this:

for name,value in test:
print name
print value

fails with of course with:
"TypeError: iteration over non-sequence"
How can I do that?

regards,
Yves
Nov 10 '05 #1
2 6027
Hi!

You can iterate over the internal dictionary:
class Test: .... def __init__(self):
.... self.x = 5
.... self.y = 6
.... self.z = "Hallo"
.... x = Test()
print x.__dict__ {'y': 6, 'x': 5, 'z': 'Hallo'} for key, value in x.__dict__.item s(): .... print key
.... print value
....
y
6
x
5
z
Hallo


Consider using iteritems() instead of items() when you have a loop.

Hope that helps :)
Daniel
Nov 10 '05 #2
Yves Glodt wrote:
I need to iterate over a class and get all her variable names and
values, e.g. considering this example:

class testclass:
var1 = 'ab'
var2 = 'cd'
var3 = 'ef'


Is the following of any help to you?
class testclass: .... a = 'a'
.... dir(testclass) ['__doc__', '__module__', 'a'] testclass.__dic t__ {'a': 'a', '__module__': '__main__', '__doc__': None} import inspect
inspect.classif y_class_attrs(t estclass)

[('__doc__', 'data', <class __main__.testcl ass at 0x00AFBE70>, None),
('__module
__', 'data', <class __main__.testcl ass at 0x00AFBE70>, '__main__'),
('a', 'data'
, <class __main__.testcl ass at 0x00AFBE70>, 'a')]
There are other methods in "inspect" which could help you.

-Peter
Nov 10 '05 #3

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

Similar topics

10
8720
by: bst | last post by:
Is there a way to iterate through member variables of different names, of course, one by one from the very first one in a while loop, for example? Thanks!
8
3643
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse, bool wordWrap ); // Resets iterator. We can ignore wordwrap for now. std::string GetLine(); // Reads a line using iterator. Increments...
3
3313
by: Matteo Cima | last post by:
hi! i have a struct like: Public struct conf { public bool debug=false; public int opPalm=-1; public int neMess=0; public bool caPart = false; }
28
1797
by: John Salerno | last post by:
What is the best way of altering something (in my case, a file) while you are iterating over it? I've tried this before by accident and got an error, naturally. I'm trying to read the lines of a file and remove all the blank ones. One solution I tried is to open the file and use readlines(), then copy that list into another variable, but...
5
29329
by: jaso | last post by:
Hi, If have a structure of a database record like this: struct record { char id; char title; ... }; Is there some way to find out how many member variables there is in the struct and then iterate through them?
1
1692
by: Steve | last post by:
Hello- I have numerous variables called varNameX (where X is equal to a number from 0 to a known number "n"). Using VB.NET, I would like to iterate through all of these variables to run comparisons. Example:
3
16493
by: Steve | last post by:
Hello- I have numerous variables called varNameX (where X is equal to a number from 0 to a known number "n"). Using VB.NET, I would like to iterate through all of these variables to run comparisons. Example:
14
2094
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each checkbox, since
6
6175
by: Bob Altman | last post by:
Hi all, In C++/CLI (VS 2005) I have a C++ module compiled with /clr that contains a module-level gcroot variable: static gcroot<MyList^m_myList = gcnew MyList; In the above statement, MyList is a managed class that inherits from Generic::List. I want to iterate through m_myList like this:
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.