1

What is the correct way in Python 3.7+ to check if a function is defined within a Python class. Example:

class A:

    def fun1(self):
        pass

def fun2():
    pass

I would like to distinguish that A.fun1 was defined within a class and fun2 is not.

6
  • You're asking magic(A.fun1) should return True and magic(fun2) should return False, right? Commented Feb 22, 2021 at 15:41
  • 4
    what is the point of doing this? what problem are you trying to solve? Commented Feb 22, 2021 at 15:41
  • There are inspect.ismethod and inspect.isfunction. ismethod should be called on object. Do these: print(inspect.ismethod(A().fun1), inspect.isfunction(A().fun1)); print(inspect.ismethod(fun2), inspect.isfunction(fun2)). Commented Feb 22, 2021 at 16:20
  • @deceze Exactly Commented Feb 22, 2021 at 16:55
  • @Arty As you pointed out, is method requires instantiating the class which might not be possible or easy or have side effects in certain cases. Commented Feb 22, 2021 at 16:56

2 Answers 2

2

You can use the attribute __qualname__ (defined in PEP 3155) and check whether it contains a . which indicates that this object was defined in a nested scope. Note that this also applies to functions defined inside other functions, so it might give you false positives in that sense.

>>> def test(): ...
... 
>>> test.__qualname__
'test'
>>> class Foo:
...     def test(self): ...
... 
>>> Foo.test.__qualname__
'Foo.test'
>>> def f():
...     def g(): ...
...     return g
... 
>>> f().__qualname__
'f.<locals>.g'
Sign up to request clarification or add additional context in comments.

Comments

-1

Here you go hasattr()

>>> hasattr(A, 'fun1')
>>> True

>>> hasattr(A, 'fun2')
>>> False

6 Comments

And how is my answer not solving that problem?
Method is a function defined within a class. Determining if class has a specific function is exactly equal to "distinguish that function was defined within a class" - as OP requested.
Well, i answer what was asked in the question, not playing a "guess what that really means" game.
In this particular case question was very clear for me, so i see no need to clarify. In fact, if you take a look at answer's at deceze's suggestion: the third answer (and 2nd most voted) is the same as mine. This concludes to me, that questions in fact are similar. And rather than doubting my answer, the appropriate action would be to flag question as duplicate. Anyway, i suggest we move on.
@go2nirvana I thought that the last phrase in my question was clarifying this: "I would like to distinguish that A.fun1 was defined within a class and fun2 is not" Which is not the same as saying if a given class defines a function with a given name.
|

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.