[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

getting all user defined attributes of a class

wildwest

2/6/2008 10:07:00 PM

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
8 Answers

Marc 'BlackJack' Rintsch

2/6/2008 10:15:00 PM

0

On Wed, 06 Feb 2008 14:07:23 -0800, Amit Gupta wrote:

> 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

wildwest

2/6/2008 10:38:00 PM

0

On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> On Wed, 06 Feb 2008 14:07:23 -0800, Amit Gupta wrote:
> > 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

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

Diez B. Roggisch

2/6/2008 10:56:00 PM

0

Amit Gupta schrieb:
> On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
>> On Wed, 06 Feb 2008 14:07:23 -0800, Amit Gupta wrote:
>>> 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
>
> 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

wildwest

2/6/2008 11:16:00 PM

0

On Feb 6, 2:55 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> Amit Gupta schrieb:
>
>
>
> > On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> >> On Wed, 06 Feb 2008 14:07:23 -0800, Amit Gupta wrote:
> >>> 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
>
> > 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

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

Marc 'BlackJack' Rintsch

2/7/2008 7:29:00 AM

0

On Wed, 06 Feb 2008 15:16:26 -0800, Amit Gupta wrote:

> On Feb 6, 2:55 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> Amit Gupta schrieb:
>> > 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
>
> 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 F

2/7/2008 8:29:00 AM

0

On Feb 6, 11:07 pm, Amit Gupta <emaila...@gmail.com> wrote:
> 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

wildwest

2/7/2008 6:43:00 PM

0

On Feb 7, 12:28 am, grflanagan <grflana...@yahoo.co.uk> wrote:
> On Feb 6, 11:07 pm, Amit Gupta <emaila...@gmail.com> wrote:
>
> > 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
[..]
>
> 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

George Sakkis

2/7/2008 7:49:00 PM

0

On Feb 7, 1:42 pm, Amit Gupta <emaila...@gmail.com> wrote:

> 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

>>> X().__dict__
{'y': 2}
>>> X.__dict__
<dictproxy object at 0xb7beab9c>

>>> X.__dict__.items()
[('__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