getting all user defined attributes of a class

A

Amit Gupta

Hi

How do I get user defined attributes of a class? e.g

Class A(object) :
self.x = 1
------------------

I want something like:
for userattrib in A.getAllUserAttribute() :
print userattrib

My question is, is there a builtin function, called
getAllUserAttributes?

Thanks
 
M

Marc 'BlackJack' Rintsch

Class A(object) :
self.x = 1

This is not valid Python code.
I want something like:
for userattrib in A.getAllUserAttribute() :
print userattrib

My question is, is there a builtin function, called
getAllUserAttributes?

No and there can't be since the attributes you seem to be interested in
don't exist until an instance is created.

Ciao,
Marc 'BlackJack' Rintsch
 
A

Amit Gupta

This is not valid Python code.



No and there can't be since the attributes you seem to be interested in
don't exist until an instance is created.

Ciao,
Marc 'BlackJack' Rintsch

My mistake:

I should make class A as:
class A (object) :
x = 1

Now, x is class attribute and I am looking for some-way to filter non-
user-defined attributes.

e.g.g if I do
for attr in a.__dict__ :
print attr


I will also get

__module__, __weakref__ and others including "x"

Thanks
 
D

Diez B. Roggisch

Amit said:
My mistake:

I should make class A as:
class A (object) :
x = 1

Now, x is class attribute and I am looking for some-way to filter non-
user-defined attributes.

e.g.g if I do
for attr in a.__dict__ :
print attr


I will also get

__module__, __weakref__ and others including "x"

Just create an empty class, gather all attribute names from that and
then subtract that set of names from the names you get from a "real" class.


Dize
 
A

Amit Gupta

Amit Gupta schrieb:










Just create an empty class, gather all attribute names from that and
then subtract that set of names from the names you get from a "real" class.

Dize

Fine. This is a hack. I am looking if python language itself provides
any built-in function for this. E.g.:

When I do help on some built-in function, it displays that function is
built_in. Can that information get accessed using a function? (now,
don't ask me to store help-output in buffer and grep for built-in).


A
 
M

Marc 'BlackJack' Rintsch

Fine. This is a hack. I am looking if python language itself provides
any built-in function for this. E.g.:

Why is that a hack!? What about:

In [369]: def is_special_name(name):
.....: return name.startswith('__') and name.endswith('__')
.....:

In [370]: filter(lambda m: not is_special_name(m[0]), inspect.getmembers(A))
Out[370]: [('x', 1)]
When I do help on some built-in function, it displays that function is
built_in. Can that information get accessed using a function? (now,
don't ask me to store help-output in buffer and grep for built-in).

Yes but it is not built-in. Have a look at the `inspect` module.

What's the use case?

Ciao,
Marc 'BlackJack' Rintsch
 
G

grflanagan

Hi

How do I get user defined attributes of a class? e.g

Class A(object) :
self.x = 1
------------------

I want something like:
for userattrib in A.getAllUserAttribute() :
print userattrib


class Meta(type):

def __init__(cls, name, bases, attrs):
super(Meta, cls).__init__(name, bases, attrs)
cls._attrs = attrs.keys()


class Base(object):
__metaclass__ = Meta

def getmembers(self):
return [k for k in self.__dict__ if k not in self._attrs]

class MyObj(Base):

def __init__(self):
self.a = 1
self.b = 2

obj = MyObj()

print obj.getmembers()

['a', 'b']

setattr(obj, 'c', 3)

print obj.getmembers()

['a', 'c', 'b']

HTH

Gerard
 
A

Amit Gupta

How do I get user defined attributes of a class? e.g
Class A(object) :
self.x = 1
------------------
I want something like:
for userattrib in A.getAllUserAttribute() :
print userattrib
[..]

HTH

Gerard

Thanks. What I found is: If I call iterate over the __dict__ of the
instance of the class, I only get user-atttributes and not built-in
attributes. I have an instance of that class, anyway, so this will do.
However, I wonder if I am getting just lucky and this might change in
future. In that regard the solution provides by all posters above
might well be more robust.

A
 
G

George Sakkis

Thanks. What I found is: If I call iterate over the __dict__ of the
instance of the class, I only get user-atttributes and not built-in
attributes. I have an instance of that class, anyway, so this will do.
However, I wonder if I am getting just lucky and this might change in
future. In that regard the solution provides by all posters above
might well be more robust.

Instances and classes have separate namespaces:

class X(object):
x = 1
def __init__(self):
self.y = 2
[('__module__', '__main__'),
('__dict__', <attribute '__dict__' of 'X' objects>),
('x', 1),
('__weakref__', <attribute '__weakref__' of 'X' objects>),
('__doc__', None),
('__init__', <function __init__ at 0xb7b5a454>)]

And neither of those includes attributes defined in superclasses,
classes with __slots__, pseudo-attributes through __getattr__ and
possibly more I've missed.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top