Exploring Attributes and Methods

K

keios.titan

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
 
T

Terry Reedy

| 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'?
['__doc__', '__module__', 'a']
 
P

Paul McGuire

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
 
T

Tim Chase

Class Sample:
fullname = 'Something'

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

with the builtin hasattr() function :)
True

-tkc
 
K

keios.titan

| 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'?

['__doc__', '__module__', 'a']

Thank You
 
D

Diez B. Roggisch

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top