Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico 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: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de j/W2lU5XT3H3aLLcyV42QAkVsa0bbLRz0lM02jukJLIw== Return-Path: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100985 On Wed, Dec 30, 2015 at 11:16 PM, Charles T. Smith 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