1

For instance, the class declaration:

class Something(Superclass):
    an_attribute = 1
    another_attribute = 'hello'

Is there any way that python can tell me that an_attribute and another_attribute were defined in this class declaration?

Another way of framing this question, I guess, is "can I filter dir results by the class in which they were declared?

alexgolec
  • 26,898
  • 33
  • 107
  • 159

6 Answers6

4

The attributes of a class are kept in it's special __dict__ field. The __dict__ contains only the attributes defined for the current class (as well as the special attributes). As others mentioned, it can be accessed either directly or via built-in vars() function.

I strongly recommend reading this article which explains how Python handles attributes in-depth.

Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
4

Python 2.7:

class A:
    var1 = 2
    va2 = 2
class B(A):
    var3 = 3
    var4 = 4

print(A.__dict__.keys()) #prints ['va2', '__module__', 'var1', '__doc__']
print(B.__dict__.keys()) #prints ['var4', '__module__', '__doc__', 'var3']

Python 3:

print(list(A.__dict__)) #prints ['__module__', 'var1', '__dict__', 'va2', '__weakref__', '__doc__']
print(list(B.__dict__)) #prints ['var4', '__module__', '__doc__', 'var3']
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    @BasicWolf There's a edit button below every post, and SO is a collaborative site so you could have used that to edit my post, instead of downvoting it. :( – Ashwini Chaudhary Jul 01 '12 at 21:33
  • 1
    With this reasoning, we wouldn't need the downvote button at all – we could *always* fix the errors instead. However, it's ridiculous to downvote because of stylistic preferences. +1 from me. – Sven Marnach Jul 01 '12 at 21:52
  • 1
    Can we save the complaints about indentation level until SO has a decent code editor. +1 from me – John La Rooy Jul 01 '12 at 23:23
  • Guys, this is my personal opinion, but check Ashwini Chaundhary's other answers @StackOverflow. 2 chars indent. Now, consider yourself some day reading that kind of code, and having no fancy IDE to re-indent it. Does it feel comfortable? The standards and particularly PEP8 were created for a sake of each one of us, it is also a part of what makes Python code readable. P.S. Ashwini Chaudhary, I see you haven't read PEP8 yet, because the code lines are longer than 80 characters :) – Zaur Nasibov Jul 02 '12 at 06:23
  • A minor improvement -- to print a list of keys in Python 3, instead of calling `keys()`, you could just do this: `print(list(A.__dict__))`. – senderle Jul 02 '12 at 15:18
2

The dictionary of attributes of the class SomeThing can be accessed by vars(SomeThing). See also the documentation of vars().

senderle
  • 145,869
  • 36
  • 209
  • 233
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
1
>>> class Something(list):
...     an_attribute = 1
...     another_attribute = 'hello'
... 
>>> print vars(Something) == Something.__dict__
True
>>> 
>>> print vars(Something)
{'__module__': '__main__', '__doc__': None, 'an_attribute': 1, '__dict__': <attribute '__dict__' of 'Something' objects>, '__weakref__': <attribute '__weakref__' of 'Something' objects>, 'another_attribute': 'hello'}
>>> 
bpgergo
  • 15,669
  • 5
  • 44
  • 68
0

Another way to do this is with introspection, using the example by @AshwiniChaudhary:

import inspect

class A:
    var1 = 2
    var2 = 2

class B(A):
    var3 = 3
    var4 = 4

[member for member in inspect.getmembers(B) if member not in inspect.getmembers(A)]
#prints [('var3', 3), ('var4', 4)]

With this approach you can decide from which class in the inheritance hierarchy you want to "subtract" the members, for example:

class One:
    #members

class Two:
    #members

...

class N:
    #members

[member for member in inspect.getmembers(N) if member not in inspect.getmembers(NMinusK)]
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
-2

There is builtin hasattr function

hasattr(object, name)

The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77
  • 2
    I don't see how this answers the question at all. Specifically, given a class `Foo` and its subclass `Bar`, `hasattr(Bar, attr)` will return true whether the attribute is defined in `Foo` or `Bar`. – senderle Jul 01 '12 at 21:15