Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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; 'classes,': 0.05; 'output': 0.05; 'args': 0.07; 'class,': 0.07; 'method.': 0.07; 'returned.': 0.07; 'suppose': 0.07; 'attributes': 0.09; 'converts': 0.09; 'latter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'def': 0.12; 'jan': 0.12; '__init__,': 0.16; '__new__': 0.16; 'attributes).': 0.16; 'b()': 0.16; 'inst': 0.16; "module's": 0.16; 'otoh,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'self.y': 0.16; 'sorts': 0.16; 'wrote:': 0.18; 'solution.': 0.20; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'skip': 0.24; 'looks': 0.24; 'sort': 0.25; 'define': 0.26; 'pass': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; '>>>>': 0.31; 'inspect': 0.31; 'object.': 0.31; 'class': 0.32; 'skip:b 30': 0.33; 'skip:_ 10': 0.34; 'subject: (': 0.35; 'objects': 0.35; 'but': 0.35; 'sequence': 0.36; 'yield': 0.36; 'method': 0.36; 'possible': 0.36; 'two': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'affect': 0.61; 'skip:a 30': 0.61; 'received:173': 0.61; 'first': 0.61; 'kind': 0.63; 'different': 0.65; 'union': 0.69; 'special': 0.74; 'hoping': 0.75; 'potentially': 0.81; 'examining': 0.84; 'received:fios.verizon.net': 0.84; 'trick,': 0.84; 'lists:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: selective (inheriting?) dir()? Date: Mon, 21 Apr 2014 17:16:47 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398115051 news.xs4all.nl 2964 [2001:888:2000:d::a6]:36453 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70476 On 4/21/2014 10:06 AM, Skip Montanaro wrote: > Before I get up to my neck in gators over this, I was hoping perhaps > someone already had a solution. Suppose I have two classes, A and B, > the latter inheriting from the former: > > class A: > def __init__(self): > self.x = 0 > > class B(A): > def __init__(self): > A.__init__(self) > self.y = 1 > > inst_b = B() > > Now, dir(inst_b) will list both 'x' and 'y' as attributes (along with > the various under under attributes). 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. OTOH, working through the code objects for the methods looks > potentially promising: > >>>> B.__init__.im_func.func_code.co_names > ('A', '__init__', 'y') >>>> A.__init__.im_func.func_code.co_names > ('x',) You can permanently affect dir(ob) with a special method. object.__dir__(self) Called when dir() is called on the object. A sequence must be returned. dir() converts the returned sequence to a list and sorts it. From outside the class, you get the attributes defined directly in a class klass as the difference of set(dir(klass) and the union of set(dir(base)) for base in klass.__bases__. To include attributes set in __new__ and __init__, replace klass with klass(). -- Terry Jan Reedy