Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to list the attributes of a class, not an object?

Reply
Thread Tools

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

 
 
Robert P. J. Day
Guest
Posts: n/a
 
      01-24-2010

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
================================================== ======================
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-24-2010
* 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
 
Reply With Quote
 
 
 
 
Robert P. J. Day
Guest
Posts: n/a
 
      01-24-2010
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?

rday
--

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

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
================================================== ======================
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-24-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
 
Reply With Quote
 
Jan Kaliszewski
Guest
Posts: n/a
 
      01-24-2010
24-01-2010, 17:37:41 Alf P. Steinbach <> wrote:

> 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:

>>> dictkeys = type({}.keys())
>>> set(dir(dictkeys)).difference(dir(object))

{'__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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
attributes ala java annotations or .Net attributes? Kyle Schmitt Ruby 3 07-24-2007 07:48 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57