Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function vs method
#1
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 ?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to use type= as a function/method keyword argument Skaperen 9 1,838 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Building a method name in a function ffgth 9 3,188 Oct-19-2020, 01:21 PM
Last Post: buran
  function/method help myv5285 3 2,793 May-17-2020, 04:19 AM
Last Post: buran
  Accessing method as function object ClassicalSoul 2 2,007 Feb-14-2020, 09:31 PM
Last Post: wavic
  I'm trying to figure out whether this is a method or function call 357mag 2 2,420 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  function method problems drchar 2 2,896 Dec-11-2018, 02:38 PM
Last Post: buran
  Understanding Method/Function robdhunter 2 2,634 Mar-10-2018, 11:57 PM
Last Post: robdhunter
  How to pass value from method to function iFunKtion 11 8,783 May-23-2017, 05:06 PM
Last Post: iFunKtion
  init vs_init_ while defining method/function? hsunteik 1 3,640 Dec-24-2016, 08:27 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020