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

Alf P. Steinbach alfps at start.no
Sun Jan 24 11:37:41 EST 2010


* Robert P. J. Day:
> On Sun, 24 Jan 2010, Alf P. Steinbach wrote:
> 
>> * Robert P. J. Day:
>>>   once again, probably a trivial question but i googled and didn't
>>> get an obvious solution.  how to list the attributes of a *class*?
>>>
>>>   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)".
>> Like,
>>
>>   dir( list )
>>
>> where 'list' is the built-in type.
>>
>> There's a pretty-printer for that somewhere, but I can't recall.
> 
>   except that doesn't work for
> 
>   >>> dir(dict_keys)
> 
> so what's the difference there?

'list' is a built-in type that by default is available.

'dict_keys' is a type that you're not meant to use directly, so it's not made 
available by default.

The 'type' function yields the type of its argument, so you *can* do e.g.

   DictKeys = type( {}.keys() )

   dir( DictKeys )
   list( vars( DictKeys ) )
   help( DictKeys )

It doesn't help much though because the only method of interrest is __iter__, 
which produces an iterator that you can use e.g. in a for loop or to construct a 
list, whatever.

The relevant place to find out more about keys() is in the documentation.


Cheers & hth.,

- Alf



More information about the Python-list mailing list