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

R

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)".

surely there's a simpler way just using the class name, no?

rday

p.s. any recommendations for the most concise reference sheet for
python 3 that exists? being able to print off the entire language
spec on two or four pages and tacking it up in front of me would be
just ducky. thanks.

--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
 
A

Alf P. Steinbach

* 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.

And as I also recommended in your thread "examining an initial, pristine python3
shell session",

help( list )

or more generally

help( "list" )

surely there's a simpler way just using the class name, no?

Yes. :)

rday

p.s. any recommendations for the most concise reference sheet for
python 3 that exists? being able to print off the entire language
spec on two or four pages and tacking it up in front of me would be
just ducky. thanks.

Sorry, don't know.


Cheers & hth.,

- Alf
 
R

Robert P. J. Day

* Robert P. J. Day:

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

so what's the difference there?

rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
 
A

Alf P. Steinbach

* Robert P. J. Day:
except that doesn't work for


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
 
J

Jan Kaliszewski

DictKeys = type( {}.keys() )

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

It doesn't help much though because the only method of interrest is
__iter__

Not only. Please, consider:
{'__ror__', '__rsub__', '__and__', '__rand__', '__contains__',
'__len__', '__iter__', '__or__', '__rxor__', '__xor__', '__sub__'}

And e.g. comparision-related mehods (__gt__, __le__ and so on) also are
more interensing in dict keys views than in plain object() instances...

Regards,

*j
 

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,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top