Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; 'output': 0.05; 'args': 0.07; 'attribute': 0.07; 'detect': 0.07; '22,': 0.09; 'attributes': 0.09; 'default.': 0.09; 'api': 0.11; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; '__init__,': 0.16; 'at.': 0.16; 'dig': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'in...': 0.16; 'inst': 0.16; "module's": 0.16; 'ought': 0.16; 'somewhere.': 0.16; 'wrote:': 0.18; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'skip': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'compiled': 0.26; 'define': 0.26; 'pass': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'inspect': 0.31; 'though.': 0.31; 'class': 0.32; 'could': 0.34; 'subject: (': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'method': 0.36; 'possible': 0.36; 'turn': 0.37; 'list': 0.37; 'that,': 0.38; 'anything': 0.39; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'kind': 0.63; 'different': 0.65; 'analysis': 0.75; 'examining': 0.84; 'trick,': 0.84; 'lists:': 0.91; 'to:none': 0.92 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=gRECK13eBLSEX7EcvaX2SFQITrFP9wXnFvy2UjlFC0M=; b=TBO4QOvV9rVRsYzmyLT3rMMFdDAppMhwgw3liXVjiFX70tNgodxHTsqyivr7Wpcz3p OGgvi3hHbQorLpMFYb4Q+l1aGLtfVan8y7sHOxIbSP3a6WaiOvi8ciasItmZlTm4fYN0 3d7ErPOgBoswq5EX8/9MCDYrJXGMEQr3ouc85m44LN5KnNeSTVP55yZ0xTb5qDMPaEQQ SchQrhQqhL5VPdtODidSaknrebWSEihsGQ88JRxGoHNuSkIk3E6Q1EPV/XN5ZPEHH+Zh 70QmFKxoLER7u7EPnjbFIgrt0TolsZg5EmiYQRFz83wQp2Rj+JnjIdYFWKRW9zleEpHI ECAA== MIME-Version: 1.0 X-Received: by 10.58.112.98 with SMTP id ip2mr358971veb.35.1398091096242; Mon, 21 Apr 2014 07:38:16 -0700 (PDT) In-Reply-To: References: Date: Tue, 22 Apr 2014 00:38:16 +1000 Subject: Re: selective (inheriting?) dir()? From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398091105 news.xs4all.nl 2884 [2001:888:2000:d::a6]:48995 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70465 On Tue, Apr 22, 2014 at 12:06 AM, Skip Montanaro wrote: > Without examining the source, is > it possible to define some kind of "selective" dir, with a API like > > def selective_dir(inst, class_): pass > > which will list only those attributes of inst which were first defined > in (some method defined by) class_? The output of calls with different > class_ args would yield different lists: > > selective_dir(inst_b, B) -> ['y'] > > selective_dir(inst_b, A) -> ['x'] > > I'm thinking some sort of gymnastics with inspect might do the trick, > but after a quick skim of that module's functions nothing leapt out at > me. Hmm. Interesting. Fundamentally, attributes in Python don't give you anything about which class they were defined in... by default. However, it ought to be possible to play around with the metaclass; it could detect that you're creating a new attribute and record that somewhere. But if you know that all the attributes you care about are set in __init__, you could do some analysis on that, as you were looking at. Might turn out to be a lot of work to dig through the compiled code, though. ChrisA