There is a printer class. He has 2 methods that do the same thing. The difference is that one of the methods is with the decorator @staticmethod , and the other is without. But I can call both methods without creating an instance of the class.
class printer(): ''' Тест @staticmethod ''' def not_static_print(self, text = 'Example Text'): print(text) @staticmethod def static_print(text = 'Example Text'): print(text) # Не создаю никаких экземпляров printer.not_static_print(None, 'Emm?') printer.static_print('Something like this.') Just for not_static_print() I specify an instance, or rather its absence ( None )
Is there a fundamental difference in the use of these methods?
p = printer()p.not_static_print(None, 'Emm?')and now try calling thatnot_static_printwith None first argument :) - gil9rednot_static_print, and this will be possible only throughself, but if you do this and passNone, then the errorAttributeError: 'NoneType' object has no attribute. It is for such cases that static methods exist - they cannot work with the fields and methods of an object, but at the same time they are not made simply as functions. they are logically related to this class / type - gil9red