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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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!
|
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...
|
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;
}
|
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...
|
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?
| |
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:
|
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:
|
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
|
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:
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |