While calling static method from inside the class (which contains the static method), it can be done in following ways:
Class.method() or self.method()
What is the difference?
What are the unique use cases for each?
class TestStatic(object):
@staticmethod
def do_something():
print 'I am static'
def use_me(self):
self.do_something() # 1st way
TestStatic.do_something() # 2nd way
t = TestStatic()
t.use_me()
prints
I am static
I am static
By using TestStatic.do_something()
you'd bypass any override on a subclass:
class SubclassStatic(TestStatic):
@staticmethod
def do_something():
print 'I am the subclass'
s = SubclassStatic()
s.use_me()
will print
I am the subclass
I am static
This may be what you wanted, or maybe it wasn't. Pick the method that best fits your expectations.
I believe that TestStatic.do_something()
is better because it demonstrates an understanding of static members. Static members are objects that belong to the class and methods that are called on the class, and in most cases, they are utility classes. Thus, calling the method via the class is more appropriate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With