If I have the following class, what\'s the best way of getting the exact list of variables and methods, excluding those from the superclass?
class F
def getVariablesClass(inst):
var = []
cls = inst.__class__
for v in cls.__dict__:
if not callable(getattr(cls, v)):
var.append(v)
return var
if you want exclude inline variables check names on the __ at the start and the end of variable
In your example, a
is an instance, its __dict__
will include all variables set in its __init__
function. To get all class variables, use a.__class__.__dict__
A third answer is the inspect module which does the same as above
If the class and its superclasses are known, something like:
tuple(set(dir(Foo)) - set(dir(Bar)))
If you want it to be more generic, you can get a list of the base classes using something like
bases = Foo.mro()
...and then use that list to subtract out attributes from all the base classes.