Skip to content
Minsing Log
Go back

English Edition

Calling a static method from a method of the same class or a different class

Programming & CS

class MyClass:
   @staticmethod
   def static_method():
       print("This is a static method in a class")
       
   def another_method(self):
       MyClass.static_method() # 동일한 클래스의 정적 메소드를 호출
class Parent:
   @staticmethod
   def static_method():
       print("부모 클래스의 정적 메소드")

class Child(Parent):
   def method(self):
       Parent.static_method() # 부모 클래스의 정적 메소드를 호출

c = Child()
c.method() # 출력: 부모 클래스의 정적 메소드

As in the example above, use the class name.method name format for a static method.

The reason for using the class name is that no instance is created. For an instance method, an instance goes in the self position.

Follow the journey

Get new posts by email.

Short, honest notes on building, startups, learning, failures, and retrospectives, for readers who follow the journey rather than pass through from search.

The newsletter is coming soon. Until then, follow via RSS or email me to get notified.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
When should you use static methods?
Next Post
Hiding Methods or Property Names by Starting Them with _ to Make Them Private