473,473 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inspecting the Instance Vars in a class/object - How?

I'm trying to understand reflection/introspection in Python. How can I
identify the the type of attribute (e.g. instance var) in a class?
The following returns all the class attributes (methods and instance
vars).

However I'm interested in identifying the type of value for each case
- (e.g. I'd like to identify the instance variables separately). (The
Inspect module has an ismethod method, but not an isinstancevariable
method).

e.g. In the following example I'd like to extract the class vars
strvar and intNum and ignore the methods/other attribute types.

What is the best way to do this?

class test:
# Dummy Class for reflection testing
strVar = '1234'
intNum = 0

def nullmethod():
pass

def addmethod(self,v1, v2):
v = v1 + v2
return v

if __name__ == "__main__":
mytest = test()
for key in dir(mytest):
value = getattr(object, key)
print 'Key: %s ; Value %s ' % (str(key) ,str(value))


Aug 27 '08 #1
1 2104
BrendanC wrote:
I'm trying to understand reflection/introspection in Python. How can I
identify the the type of attribute (e.g. instance var) in a class?
The following returns all the class attributes (methods and instance
vars).

However I'm interested in identifying the type of value for each case
- (e.g. I'd like to identify the instance variables separately). (The
Inspect module has an ismethod method, but not an isinstancevariable
method).
Because there is no such thing, as it is not a *type*. It's a question on
where the value is stored.

Every *instance* has a dictionary holding it's data. You can normally access
it using __dict__.

But it's not "pure" in the sense that only the variables you created
yourself are contained. It also contains the reference to the class.

On the class, there is also a __dict__, which contains the
method-descriptors and class-variables.

In the face of multi-inheritance, things get even more complicated, as then
the values are acquired through MRO-lookup.
e.g. In the following example I'd like to extract the class vars
strvar and intNum and ignore the methods/other attribute types.

What is the best way to do this?

class test:
# Dummy Class for reflection testing
strVar = '1234'
intNum = 0

def nullmethod():
pass

def addmethod(self,v1, v2):
v = v1 + v2
return v

if __name__ == "__main__":
mytest = test()
for key in dir(mytest):
value = getattr(object, key)
print 'Key: %s ; Value %s ' % (str(key) ,str(value))
if name in mytest.__dict__:
print "instance variable"

Aug 27 '08 #2

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

Similar topics

6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
3
by: wolfgang.lipp | last post by:
some time after posting my `Linkdict recipe`__ to aspn__ -- basically, a dictionary with run-time delegational lookup, but this is not important here -- i thought gee that would be fun to make...
6
by: Bran Kelly | last post by:
Hi, I am running into something I haven't ever encountered before. I have a static instance of a class declared in a cpp file, which seems to be optimized (or for some other reason) out of...
7
true911m
by: true911m | last post by:
I'm getting ready to launch another long-winded project inquiry for feedback from you guys, cause I'm still working on the finer points of class implementation in my stubborn head. While I was...
9
by: glomde | last post by:
Hi I wonder if you can set what subclass a class should have at instance creation. The problem is that I have something like: class CoreLang(): def AssignVar(self, var, value): pass class...
7
by: JonathanB | last post by:
Ok, I know there has to be a way to do this, but my google-fu fails me (once more). I have a class with instance variables (or should they be called attributes, I'm newish to programming and get...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
0
by: Miles | last post by:
On Mon, Sep 15, 2008 at 6:06 AM, Harish K Vishwanath <harish.shastry@gmail.comwrote: "built-in type" generally means "implemented in C", also sometimes called "extension type". Both the...
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
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...
0
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.