Nov-14-2019, 01:54 PM
(This post was last modified: Nov-14-2019, 01:54 PM by ichabod801.)
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() # dittoEdit: 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
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures