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

Discussion in 'Python' started by Robert P. J. Day, Jan 24, 2010.

  1. 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
    ========================================================================
     
    Robert P. J. Day, Jan 24, 2010
    #1
    1. Advertising

  2. * 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
     
    Alf P. Steinbach, Jan 24, 2010
    #2
    1. Advertising

  3. 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
    ========================================================================
     
    Robert P. J. Day, Jan 24, 2010
    #3
  4. * 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
     
    Alf P. Steinbach, Jan 24, 2010
    #4
  5. 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
     
    Jan Kaliszewski, Jan 24, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,940
    Thomas Weidenfeller
    Oct 12, 2005
  2. Max
    Replies:
    1
    Views:
    515
    Joe Kesselman
    Sep 22, 2006
  3. P4trykx
    Replies:
    2
    Views:
    1,890
    bruce barker
    Jan 31, 2007
  4. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    412
    Bruno Desthuilliers
    Jun 20, 2007
  5. Kyle Schmitt
    Replies:
    3
    Views:
    225
    Kyle Schmitt
    Jul 24, 2007
Loading...

Share This Page