This article is participating in Python Theme Month. See the link for details

preface

Python has been an object-oriented language since its design, and because of that, it’s easy to create classes and objects in Python. We’ll cover object-oriented programming in Python in detail.

Introduction to object oriented technology

  • Class: A collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
  • Method: a function defined in a class.
  • Class variables: Class variables are common throughout the instantiated object. Class variables are defined in the class and outside the function body. Class variables are not usually used as instance variables.
  • Data members: Class variables or instance variables are used to process data related to a class and its instance objects.
  • Method override: If a method inherited from a parent class does not meet the needs of a subclass, it can be overridden. This process is called method override, also known as method rewriting.
  • Local variables: Variables defined in a method that apply only to the class of the current instance.
  • Instance variables: In the declaration of a class, properties are represented by variables called instance variables, which are a variable decorated with self.
  • Inheritance: A derived class inherits the fields and methods of the base class. Inheritance also allows an object of a derived class to be treated as a base class object. For example, there is a design where an object of type Dog is derived from the Animal class, which emulates the “is an (IS-A)” relationship (example: Dog is an Animal).
  • Instantiation: Create an instance of a class, a concrete object of that class.
  • Object: Instance of a data structure defined by a class. An object consists of two data members (class variables and instance variables) and methods.

In contrast to other programming languages, Python adds classes with as little new syntax and semantics as possible.

Class object

# Custom classesclass MyClass:
    j = 12345
    def f(self):
        return 'hello world'X = MyClass()"The attribute J of MyClass is:", x.j)
print("MyClass method f output:", x.f())

Copy the code

inheritance

Inheritance includes single inheritance and multiple inheritance. This article introduces single inheritance.

Classes contain basic attributes, private attributes, and constructors, just as in an object-oriented language.

# class definitionclass people: # Basic attribute # Namename = ' '# age =0Private attributes cannot be accessed directly from outside the class0Def __init__(self,n,a,w): self. Name = n self.age = a self."% S said: I am % D."%(self.name,self.age)) #class student(people): # child attributegrade = ' '__init__(self,n,a,w,g): def say(self,n,a,w) : print()"% S says: I am % D and I am in % D grade"%(self.name,self.age,self.grade))
Copy the code

Class proprietary methods:

  • _init_ : constructor, called when the object is generated
  • _del_ : destructor used when releasing objects
  • _rePR_ : print, convert
  • _setiTEM_ : The value is assigned according to the index
  • _getiTEM_ : Obtains the value by index
  • _len_: Gets the length
  • _cmp_: comparison operation
  • _call_: function call
  • _add_ : add operation
  • _sub_ : subtraction operation
  • _mul_ : by operation
  • _truediv_ : divide
  • _mod_: complements
  • _pow_ : power