+ 1

In this code, how can we distinguish function from method?

class A: def spam(self): print(1) class B(A): def spam(self): print(2) super().spam() B().spam() Maybe this is a stereotype but I think that spam in class A block is defined as a function, rather than a method. However, super().spam() indicates spam() is a method by appending a dot. I know all methods are function but vice versa?? I'm not down with that.

28th Oct 2016, 1:04 AM
Eunjae Lee
Eunjae Lee - avatar
2 ответов
+ 2
a 'method' is really just a function, but assigned to a class. it acts like any other function, although, with some differences. method always has 'self' argument, and it cannot be called without referencing to a class containing it. also they are used to make some actions with objects that are instances of this class, like changing some private properties. it doesn't matter using function or class method if you need to do something simple like printing or finding the sum of a list (and often you don't need more than just a function), but if your program is more complicated and object-oriented, defining some functions as methods would be more convenient for interacting with objects. method examples in tutorial may be simple and not logical for real using, but it is made just for understanding concept on basic things, like "you can do such things, but try on some stupid and simple, or your head will explode"
29th Oct 2016, 11:22 PM
Demeth
Demeth - avatar
0
Thanks!!!
30th Oct 2016, 12:30 AM
Eunjae Lee
Eunjae Lee - avatar