class static variables and __dict__

7stud bbxx789_05ss at yahoo.com
Sat Feb 16 22:27:56 EST 2008


On Feb 16, 5:03 pm, Zack <gol... at gmail.com> wrote:
> Dustan wrote:
> > On Feb 16, 4:40 pm, Zack <gol... at gmail.com> wrote:
> >> what method can you use on x to find all available
> >> attributes for that class?
>
> >>>> class Foo(object):
> >    bar = "hello, world!"
> >    def __init__(self, baz):
> >            self.baz = baz
>
> >>>> x = Foo(42)
>
> >>>> x.__dict__.keys() # Does not include bar
> > ['baz']
>
> >>>> dir(x) # Includes bar plus some methods
> > ['__class__', '__delattr__', '__dict__', '__doc__',
> > '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
> > '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> > '__weakref__', 'bar', 'baz']
>
> I knew there was something simple I was forgetting.
> Thanks
>

dir() doesn't do it either:

-----
dir(	[object])
Without arguments, return the list of names in the current local
symbol table. With an argument, attempts to return a list of valid
attributes for that object. This information is gleaned from the
object's __dict__ attribute, if defined, and from the class or type
object. ***The list is not necessarily complete.*** If the object is a
module object, the list contains the names of the module's attributes.
If the object is a type or class object, the list contains the names
of its attributes, and recursively of the attributes of its bases.
Otherwise, the list contains the object's attributes' names, the names
of its class's attributes, and recursively of the attributes of its
class's base classes. The resulting list is sorted alphabetically. For
example:

Note: Because dir() is supplied primarily as a convenience for use at
an interactive prompt, it tries to supply an interesting set of names
more than it tries to supply a rigorously or consistently defined set
of names, and its detailed behavior may change across releases.
--------------

In other words, dir() is the most preposterous function in python.



More information about the Python-list mailing list