[Tutor] Difference(s) betweenPython 3 static methods with and without @staticmethod?

boB Stepp robertvstepp at gmail.com
Sun Aug 6 19:35:10 EDT 2017


I am looking more deeply into the subject of decorators for a bit (So
I can thoroughly digest Steve's information in the thread "basic
decorator question".) and have been first looking at static and class
methods.  As a start, I have tried the following:

=========================================================================================
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
py3: class MyClass:
...     def my_method():
...             print('This is my_method in MyClass!')
...
py3: class MyOtherClass:
...     @staticmethod
...     def my_other_method():
...             print('This is my_other_method in MyOtherClass!')
...
py3: MyClass.my_method()
This is my_method in MyClass!
py3: MyOtherClass.my_other_method()
This is my_other_method in MyOtherClass!
=========================================================================================

I see no difference in result, whether I use the @staticmethod decorator or not.

In "Python Pocket Reference, 5th ed." by Mark Lutz, on page 151 under
the entry "staticmethod(function)" the author states, "... Use the
@staticmethod functiion decorator in version 2.4 and later ... In
Python 3.X only, this built-in is not required for simple functions in
classes called only through class objects (and never through instance
objects)."  So what I am understanding is that I only need use the
@staticmethod decorator if I am using Python versions 2.4 through 2.7
(new-style classes).  Is my understanding correct?

TIA!

-- 
boB


More information about the Tutor mailing list