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


Groups > comp.lang.python > #70465 > unrolled thread

Re: selective (inheriting?) dir()?

Started byChris Angelico <rosuav@gmail.com>
First post2014-04-22 00:38 +1000
Last post2014-04-22 00:38 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: selective (inheriting?) dir()? Chris Angelico <rosuav@gmail.com> - 2014-04-22 00:38 +1000

#70465 — Re: selective (inheriting?) dir()?

FromChris Angelico <rosuav@gmail.com>
Date2014-04-22 00:38 +1000
SubjectRe: selective (inheriting?) dir()?
Message-ID<mailman.9410.1398091105.18130.python-list@python.org>
On Tue, Apr 22, 2014 at 12:06 AM, Skip Montanaro <skip@pobox.com> 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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web