Re: getting all user defined attributes of a class

2008-02-07 Thread George Sakkis
On Feb 7, 1:42 pm, Amit Gupta <[EMAIL PROTECTED]> 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__


>>> X.__dict__.items()
[('__module__', '__main__'),
 ('__dict__', ),
 ('x', 1),
 ('__weakref__', ),
 ('__doc__', None),
 ('__init__', )]

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

George
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-07 Thread Amit Gupta
On Feb 7, 12:28 am, grflanagan <[EMAIL PROTECTED]> wrote:
> On Feb 6, 11:07 pm, Amit Gupta <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-07 Thread grflanagan
On Feb 6, 11:07 pm, Amit Gupta <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Marc 'BlackJack' Rintsch
On Wed, 06 Feb 2008 15:16:26 -0800, Amit Gupta wrote:

> On Feb 6, 2:55 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Amit Gupta
On Feb 6, 2:55 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Amit Gupta schrieb:
>
>
>
> > On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Diez B. Roggisch
Amit Gupta schrieb:
> On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Amit Gupta
On Feb 6, 2:15 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting all user defined attributes of a class

2008-02-06 Thread Marc 'BlackJack' Rintsch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


getting all user defined attributes of a class

2008-02-06 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list