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


Groups > comp.lang.python > #100985

Re: how to get names of attributes

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: how to get names of attributes
Date Wed, 30 Dec 2015 23:34:03 +1100
Lines 37
Message-ID <mailman.68.1451478846.11925.python-list@python.org> (permalink)
References <n60gfk$b0t$1@dont-email.me> <n60hv9$hln$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de j/W2lU5XT3H3aLLcyV42QAkVsa0bbLRz0lM02jukJLIw==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'received:209.85.223': 0.03; 'cc:addr:python-list': 0.09; 'level:': 0.09; 'matched': 0.09; 'def': 0.13; 'wed,': 0.15; 'cls': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mro': 0.16; 'received:209.85.223.173': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'skip:v 30': 0.20; 'pass': 0.22; 'dec': 0.23; 'header:In-Reply- To:1': 0.24; "i've": 0.25; 'message-id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; "i'm": 0.30; 'class': 0.33; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:v 20': 0.38; 'received:209': 0.38; 'skip:o 20': 0.38; 'sure': 0.39; '30,': 0.63; 'complete': 0.63; '100%': 0.72; 'smith': 0.76; 'subject:get': 0.81; 'discovered': 0.83; 'chrisa': 0.84; 'glad': 0.87; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=VCrj+m6anHBxuGU398GSyu7kjgTaquvMYK+KNV7v5nI=; b=rvB+TGsJezS17wmMf258Zi8fKnhpm9/h72HTKKzuPZoMZn7eReckRRBPe17jXE/is/ jOkxPIBelf2uh3k4l7V7JVzfGPwnLUUyYXxuJjDkuHet60AKQTuyUnoi/6Q6e4inLln7 dJzxriSmnaDH8PNrZcFimFHCv2zyEVNHuLY1gkCePb2hRNJKHEbPuRigYh3lpzRmPxO1 ZbsfE1jAxrPgwJaRHP9Hugnm2kom4mHldsTtNgc2HZJM91u8CJk46HIzYRhrV6BM3LaE eE0jd5NNQW37CqFMxEii0K5+f79PeQDiiI/ikWoMdt0FzhmX7QVSAdhtZUxQBcre6fi/ O72Q==
X-Received by 10.107.40.76 with SMTP id o73mr2712250ioo.157.1451478843367; Wed, 30 Dec 2015 04:34:03 -0800 (PST)
In-Reply-To <n60hv9$hln$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100985

Show key headers only | View raw


On Wed, Dec 30, 2015 at 11:16 PM, Charles T. Smith
<cts.private.yahoo@gmail.com> wrote:
> I'm glad I discovered __mro__(), but how can I do the same thing for old-
> style classes?

You should be able to track through __bases__ and use vars() at every level:

>>> class X: pass
...
>>> class Y(X): pass
...
>>> class Z(Y): pass
...
>>> X.x=1
>>> Y.y=2
>>> Z.z=3
>>> inst=Z()
>>> inst.i=4
>>> def class_vars(old_style_class):
...     v = {}
...     for cls in old_style_class.__bases__:
...         v.update(class_vars(cls))
...     v.update(vars(old_style_class))
...     return v
...
>>> def all_vars(old_style_inst):
...     v = class_vars(old_style_inst.__class__)
...     v.update(vars(old_style_inst))
...     return v
...
>>> all_vars(inst)
{'i': 4, '__module__': '__main__', 'y': 2, 'x': 1, 'z': 3, '__doc__': None}

I'm not 100% sure I've matched the MRO here, but if all you want is
the complete set of attribute names, this should work - I think.

ChrisA

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