how to list the attributes of a class, not an object?

Jan Kaliszewski zuo at chopin.edu.pl
Sun Jan 24 10:56:42 EST 2010


24-01-2010, 16:28:26 Robert P. J. Day <rpjday at crashcourse.ca> wrote

>   once again, probably a trivial question but i googled and didn't
> get an obvious solution.  how to list the attributes of a *class*?

     dir(type(an_obj))

or more reliable:

     list(vars(type(an_obj)))

(dir() uses __dir__ which can be implemented in any way, and default  
implementation, accordinto to the docs, "attempts to produce the most  
relevant, rather than complete, information").

>   eg., i was playing with dicts and noticed that the type returned by
> the keys() method was "dict_keys".  so i'm now curious as to the
> attributes of the dict_keys class.  but i don't know how to look at
> that without first *creating* such an instance, then asking for
> "dir(dk)".

Why you bother about creating an instance? Just do it:

list(vars(type({}.keys())))

or dir(type({}.keys()))
if dir() satisfies you.

Regards,
*j

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list