0

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
  • 3
    Can you give an example of what attributes you don't want to see? Commented Feb 12, 2012 at 12:50

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Superb! This is perfect. tks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.