Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; 'classes,': 0.05; 'output': 0.05; 'args': 0.07; 'suppose': 0.07; 'attributes': 0.09; 'latter': 0.09; 'api': 0.11; 'def': 0.12; 'attributes).': 0.16; 'b()': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'inst': 0.16; "module's": 0.16; 'otoh,': 0.16; 'self.y': 0.16; 'sender:addr:gmail.com': 0.17; 'solution.': 0.20; '>>>': 0.22; 'skip': 0.24; 'looks': 0.24; 'sort': 0.25; 'define': 0.26; 'pass': 0.26; 'defined': 0.27; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'inspect': 0.31; 'class': 0.32; 'skip:b 30': 0.33; 'skip:_ 10': 0.34; 'subject: (': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'method': 0.36; 'possible': 0.36; 'two': 0.37; 'list': 0.37; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'skip:a 30': 0.61; 'first': 0.61; 'kind': 0.63; 'different': 0.65; 'hoping': 0.75; 'potentially': 0.81; 'examining': 0.84; 'trick,': 0.84; 'lists:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:message-id:subject:from:to:content-type; bh=mWK9OMS66l17MmyVi/2oexIiVsNNlzwlLG8CTSDLEp0=; b=K+MaKUYlKSdLNMcy+zELAtZt3MhdIID9t/JFAnu832W1ZW+LqThzXex23i8Cyael3V 0bZohgqSKAglHcp/rB75GrjeD+suATvrTAIee/Yp3O14ncrx8Uiv7l5JohRQ4yJy7H5w E40VVbEdRmaC28bRTJND9zHFXPZhhHWYJfjmFhx+2iJZsMpdQ+YVemYBAyF7yzkdaz3G 69d7+onDp16AAd5JhMAyTt8lPzfclxWGQAWePzeuRcKdH7JTw+zEHpITM/+AehMstqLA /UuiqxPAzZ2p8gERNl/2U+ueAwAR3Qr/F8LBY0uHIHLKq1Gbv6e9MwyGe/s/9etUnbSS mHPg== MIME-Version: 1.0 X-Received: by 10.50.82.73 with SMTP id g9mr22701877igy.0.1398089174139; Mon, 21 Apr 2014 07:06:14 -0700 (PDT) Sender: skip.montanaro@gmail.com Date: Mon, 21 Apr 2014 09:06:14 -0500 X-Google-Sender-Auth: -DLdv7Imz0H5Ln5c3Zrgj5bzi9I Subject: selective (inheriting?) dir()? From: Skip Montanaro To: 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398089649 news.xs4all.nl 2965 [2001:888:2000:d::a6]:42731 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70463 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',) Thx, Skip