FLYNNLABAboutMeCodingActivityStudy 2024초등수학
check callable parameter in python
python

python에서 classmethod와 staticmethod 차이점은 첫번째 파라미터에 cls가 넘어오느냐 아니냐의 차이였다고 생각했는데 아래와 같은 동작 차이도 있었다.

호출가능여부를 체크할때에는 callable을 활용해야겠다.

import inspect

class ATest:
  @classmethod
  def test_classmethod(cls):
    pass
  @staticmethod
  def test_staticmethod():
    pass

inspect.isfunction(ATest.test_classmethod) # False
inspect.ismethod(ATest.test_classmethod) # True
callable(ATest.test_classmethod) # True

inspect.isfunction(ATest.test_staticmethod) # True
inspect.ismethod(ATest.test_staticmethod) # False
callable(ATest.test_staticmethod) # True

check in python 3.6+