Python Forum

Full Version: function vs method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I am new to python , i was reading about function and method and it seems to me that function is like static method in java (kind of , no need to refer class to call this method.) and method is like class instance method.

You can call function without referring which class this function belong to.

Is this right approach to think ?
In Python, a function is just a function. It does not belong to any class. In addition, you can have methods, which are part of the namespace of a class. In addition to the usual methods, you can have static methods and class methods. All methods can be accessed through the namespace of a class or the namespace of an instance. They are differentiated by the automatic arguments passed. A standard method automatically passes the instance as the first parameter (if it is bound), a class method automatically passes the class itself as the first parameter, and a static method doesn't automatically pass anything. As in noted, standard methods are bound to their instances. So if you access it from the instance, the automatic parameter passing will occur. But you can access it from the class namespace, although the automatic parameter passing won't happen: you will have to explicitly pass something in place of the instance.

def wow():  # function
    pass

class Big(object):

    def ba(self):  # standard method, self is the instance
        pass

    @classmethod
    def da(cls):   # class method, cls is the class
        pass

    @staticmethod
    def boom():    # static method
        pass

wow()          # don't need to reference a class or instance
foo = Big()    # class instance
foo.ba()       # self (foo) gets automatically passed
Big.ba(foo)    # you have to pass self explicitly
foo.da()       # cls (Big) gets automatically passed
Big.da()       # ditto
foo.boom()     # nothing extra is passed
Big.boom()     # ditto
Edit: Since you seem to be coming from Java, let me note that in Python you rarely, very rarely, maybe never, write getters and setters. Just FYI.
Functions don't keep the state from the last call.
If you call them, the function itself has no information about the call before.


Special case of functions.


Methods are also functions, but the instance of the class can memorize the state.
The method can access to the current state.

It's often the case, that you start with plain functions and later you recognize, that some of the used functions need access to variables in other functions. Then you refactor your code and put those functions in a class. Then you can share across the methods the needed variables (right term is: names, which refer to objects). This happens for example if you work with tkinter (integrated GUI Framework). Many examples do not use classes together with tkinter, but if the program get more complex, classes will help.