Getting not derived members of a class

George Sakkis gsakkis at rutgers.edu
Mon Aug 1 10:43:22 EDT 2005


Franz Steinhaeusler wrote:

> Hello NG,
>
> I want to retrieve the members of a class
> with a baseclass.
> But the problem is, how to get the non derived
> members.
>
> class a:
>     def who(self):
>         print "who"
>     def __init__(self):
>         self._a = 3
>
> class b(a):
>     def who1(self):
>         print "who1"
>     def __init__(self):
>         a.__init__(self)
>         self._b = 4
>
> y=b()
>
> dir (y)
> ['__doc__', '__init__', '__module__', '_a', '_b', 'who', 'who1']
>
>
> I need a function which lists only the members of
> the "not derived" class (here class B).
>
> _b
> _who1
> __init__
>
> How can I achieve this?
> With the introspect module or so?

I believe you can't: Both _a and _b end up in y.__dict__ and there's no
way to differentiate between the two depending on the time of their
creation. By the way, these are instance attributes, not class
attributes, so strictly _b is not a member of B, it's just an instance
of y. To see why this is the case, check the following valid (though
highly discouraged) example:

class X:
    def __init__(self, x):
        if isinstance(x,str):
            self._s = x
        else:
            self._n = x

x1 = X("1")
x2 = X(1)

dir(x1)
['__doc__', '__init__', '__module__', '_s']

dir(x2)
['__doc__', '__init__', '__module__', '_n']


George




More information about the Python-list mailing list