Classes such as python foundation tutorials define usage methods

  • 2020-04-02 13:26:23
  • OfStack

Objects in objects (oop) are a very important point, and we can think of them simply as a collection of data and methods that access and manipulate that data. After learning about functions, why replace functions with classes when you can reuse code?
Classes have some advantages

1) class objects are polymorphic: that is, they have many forms, which means that we can use the same operation method for different class objects without writing extra code.
2) encapsulation of the class: after encapsulation, the object of the class can be directly called to operate some internal class methods, without the need to let the user see the details of the code work.
3) class inheritance: classes can inherit their methods from other classes or metaclasses and use them directly.

Define the syntax for a class


>>> class Iplaypython:
>>>     def fname(self, name):
>>>           self.name = name

Look at the first line, the syntax is class followed by the name of the class, and don't forget the colon at the end to define a class.
Snake tip: the name of the class, the first letter, there is a non-text rule, preferably capitalized, so you need to identify in the code to distinguish each class.
The second line starts with the method of the class, which you can see is very similar to a function, but unlike a normal function, it has a "self" argument inside, which is a reference to the object itself.

Source address: (link: http://www.iplaypython.com/jichu/class.html)


Related articles: