On Sun, 17 Mar 2019 at 18:18, Arup Rakshit <a...@zeit.io> wrote: > > I am reading a book where the author says that: > > In principle, it would also be possible to implement any @staticmethod > completely outside of the class at module scope without any loss of > functionality — so you may want to consider carefully whether a particular > function should be a module scope function or a static method. The > @staticmethod decorator merely facilitates a particular organisation of the > code allowing us to place what could otherwise be free functions within > classes. > > I didn’t get quiet well this block of text. My first question is how would I > make a module level function as static method of a class. Can anyone give me > an example of this? What are the contexts that would let you to think if they > are good fit inside the class or module level scope functions?
The point the author is trying to make is that there's no practical difference between def say_hello(name): print("Hello,", name) and class Talker: @staticmethod def say_hello(name): print("Hello,", name) You refer to the first as "say_hello", and the second as "Talker.say_hello", but otherwise they are used identically. The static method has no access to the class or instance variables, so it has no special capabilities that the standalone "say_hello" function has. So, to rephrase the words you used, @staticmethod lets you organise your code in a certain way, but doesn't offer any extra capabilities over module-level functions. Paul -- https://mail.python.org/mailman/listinfo/python-list