The "dir()" function in python retrieves all attributes for a class. I was wondering if there was a similar function that returns only user defined functions? Thanks!
1 Answer
If you want to tell a builtin from a user-defined function I'd use the types module. For instance:
>>> def hello():
... print("hi")
...
>>> import types
>>> type(hello) is types.BuiltinFunctionType
False
>>> type(hello) is types.FunctionType
True
Then it depends on what you want to do.You could use list comprehensions to check all attributes of a class and keep only those that turn out to be true.
[ x for x in dir(yourclass) if (type(x) is types.FunctionType) ]
Hope it helps.
1 Comment
delita
Superb! This is perfect. tks!