Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100984

Re: how to get names of attributes

From "Charles T. Smith" <cts.private.yahoo@gmail.com>
Newsgroups comp.lang.python
Subject Re: how to get names of attributes
Date 2015-12-30 12:16 +0000
Organization A noiseless patient Spider
Message-ID <n60hv9$hln$1@dont-email.me> (permalink)
References <n60gfk$b0t$1@dont-email.me>

Show all headers | View raw


On Wed, 30 Dec 2015 11:51:19 +0000, Charles T. Smith wrote:

> Hi,
> 
> How can I get *all* the names of an object's attributes?  I have legacy
> code with mixed new style classes and old style classes and I need to
> write methods which deal with both.  That's the immediate problem, but
> I'm always running into the need to understand how objects are linked,
> in particular when in pdb.  The answers one always sees on StackOverflow
> is that you don't need to understand, understanding is not the pythonic
> way to do things.
> 
> Alternatively, is there are map documented somewhere - more complete
> than python/python-2.7.3-docs-html/library/stdtypes.html?
> highlight=class#special-attributes
> 
> Or, is the code available uncompiled somewhere on my machine?
> 
> Does anyone know *why* the __members__ method was deprecated, to be
> replaced by dir(), which doesn't tell the truth (if only it took an
> optional parameter to say: "be truthful")
> 
> cts


For example:

    (PDB)pp dir   (newclass.__class__)
    ['__class__',
     '__delattr__',
     '__dict__',
     '__doc__',
     '__format__',
     '__getattribute__',
     '__hash__',
     '__init__',
     '__module__',
     '__new__',
     '__reduce__',
     '__reduce_ex__',
     '__repr__',
     '__setattr__',
     '__sizeof__',
     '__str__',
     '__subclasshook__',
     '__weakref__',
     'm2']

    (PDB)pp dir   (oldclass.__class__)
    ['__doc__', '__module__', 'm3']

    (PDB)pp    (oldclass.__class__.__name__)
    'C3'

    (PDB)pp    (newclass.__class__.__name__)
    'C2'

Both dir() invocations are lying to me.  The old-style class even ignores 
the pretty-print command.

I'm glad I discovered __mro__(), but how can I do the same thing for old-
style classes?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to get names of attributes "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 11:51 +0000
  Re: how to get names of attributes Chris Angelico <rosuav@gmail.com> - 2015-12-30 22:58 +1100
  Re: how to get names of attributes "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 12:16 +0000
    Re: how to get names of attributes Chris Angelico <rosuav@gmail.com> - 2015-12-30 23:34 +1100
  Re: how to get names of attributes "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 12:40 +0000
    Re: how to get names of attributes Chris Angelico <rosuav@gmail.com> - 2015-12-30 23:50 +1100
      Re: how to get names of attributes "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 13:31 +0000
        Re: how to get names of attributes Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-30 14:16 +0000
        Re: how to get names of attributes Chris Angelico <rosuav@gmail.com> - 2015-12-31 00:45 +1100
    Re: how to get names of attributes Random832 <random832@fastmail.com> - 2015-12-30 12:04 -0500
    Re: how to get names of attributes Chris Angelico <rosuav@gmail.com> - 2015-12-31 09:26 +1100
  Re: how to get names of attributes Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-30 14:10 +0000
    Re: how to get names of attributes "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 14:50 +0000
  Re: how to get names of attributes Steven D'Aprano <steve@pearwood.info> - 2015-12-31 10:58 +1100
    Re: how to get names of attributes "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 10:46 +0000

csiph-web