[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Exploring Attributes and Methods

keios.titan@gmail.com

3/6/2008 7:15:00 PM

Hi,
Is there a python command that allows me to extract the names (not
values) of the attributes of a class.

example

Class Sample:
fullname = 'Something'

How can I know that this class has an attribute called 'fullname'?

I hope my question is clear.
Thanks


5 Answers

Terry Reedy

3/6/2008 7:30:00 PM

0


<keios.titan@gmail.com> wrote in message
news:6018ff8f-3372-42c2-9a2e-5ca996744d5b@e6g2000prf.googlegroups.com...
| Hi,
| Is there a python command that allows me to extract the names (not
| values) of the attributes of a class.
|
| example
|
| Class Sample:
| fullname = 'Something'
|
| How can I know that this class has an attribute called 'fullname'?

>>> class C: a = 1

>>> dir(C)
['__doc__', '__module__', 'a']



Paul McGuire

3/6/2008 7:35:00 PM

0

On Mar 6, 1:14 pm, "keios.ti...@gmail.com" <keios.ti...@gmail.com>
wrote:
> Hi,
>  Is there a python command that allows me to extract the names (not
> values) of the attributes of a class.
>
> example
>
> Class Sample:
>     fullname = 'Something'
>
> How can I know that this class has an attribute called 'fullname'?
>
> I hope my question is clear.
> Thanks

If you had posted actual working code, "Class" would be all lower case
("class"). But we get the drift (in general, though, when posting
questions about examples from your code, best to copy-paste the actual
code, not a paraphrasing of it):

print Sample.__dict__.keys()

prints:

['__module__', 'fullname', '__doc__']


-- Paul

Tim Chase

3/6/2008 7:40:00 PM

0

> Class Sample:
> fullname = 'Something'
>
> How can I know that this class has an attribute called 'fullname'?

with the builtin hasattr() function :)

>>> class Sample: fullname='Something'
....
>>> hasattr(Sample, 'fullname')
True

-tkc


keios.titan@gmail.com

3/6/2008 7:42:00 PM

0

On Mar 7, 12:30 am, "Terry Reedy" <tjre...@udel.edu> wrote:
> <keios.ti...@gmail.com> wrote in message
>
> news:6018ff8f-3372-42c2-9a2e-5ca996744d5b@e6g2000prf.googlegroups.com...
> | Hi,
> | Is there a python command that allows me to extract the names (not
> | values) of the attributes of a class.
> |
> | example
> |
> | Class Sample:
> | fullname = 'Something'
> |
> | How can I know that this class has an attribute called 'fullname'?
>
> >>> class C: a = 1
> >>> dir(C)
>
> ['__doc__', '__module__', 'a']

Thank You

Diez B. Roggisch

3/6/2008 8:20:00 PM

0

keios.titan@gmail.com schrieb:
> Hi,
> Is there a python command that allows me to extract the names (not
> values) of the attributes of a class.
>
> example
>
> Class Sample:
> fullname = 'Something'
>
> How can I know that this class has an attribute called 'fullname'?
>
> I hope my question is clear.

The question others have answered already. But I have one for you: you
are aware that these kind of variables are *class variables*, NOT
*instance variables*, as they are created like this:


class Foo(object):
def __init__(self):
self.fullname = "something"


Diez