What is the difference between Python between a class call and a method call?

Being probably one of the worst OOP programmers on the planet, I read a lot of code examples to help get what the class can be used for. I recently found this example:

class NextClass:                            # define class
    def printer(self, text):                # define method
        self.message = text                 # change instance
        print self.message                  # access instance

x = NextClass()                             # make instance

x.printer('instance call')              # call its method

print x.message                               # instance changed

NextClass.printer(x, 'class call')      # direct class call

print x.message                               # instance changed again

There is no difference between what a direct class call does and an instance call; but he goes against Zen to include features that are not used by them. So, if there is a difference, what is it? Performance? Overhead reduction? Maybe readability?

+3
source share
3 answers

There is no difference. instance.method(...) class.method(instance, ...). But this does not contradict Zen, as it says (my attention):

- - .

, , Python, ( ), , .

? , - - , / (, , ). , (, this ++/Java/D), Zen , " , ", self , . .

, , () , - - , , - , .

+2

. :

x.printer('instance call')  # you supplied x and then called its printer method
NextClass.printer(x, 'class call')  # you supplied x as a parameter this time

, , . , . , :

car.drive('place')
car.refuel
car.impound

( ):

Car.numberintheworld # returns the number of cars in the world (or your program)

, .

+1

.

, , . , , () (.. ). , , .

0

Source: https://habr.com/ru/post/1760100/


All Articles