SoFunction
Updated on 2024-11-15

python class definitions explained

I. Class definitions:
Copy Code The code is as follows.

class <class name>.
<statement>.

After a class is instantiated, its properties can be used; in fact, after a class is created, its properties can be accessed through the class name. If you modify the properties directly using the class name, it will directly affect the instantiated objects.

Private attributes of the class:
__private_attrs Two underscores at the beginning of the attribute declare that the attribute is private and cannot be used or accessed directly from outside the class. When used in a method inside a class self.__private_attrs
Class methods
Inside a class, you can define a method for the class using the def keyword. Unlike normal function definitions, a class method must contain the parameter self as the first argument.
Private class methods
__private_method Two underscores at the beginning declare that the method is private and cannot be called from outside the class. Calling slef.__private_methods from inside a class

class's proprietary methods:
__init__ constructor, called when generating the object
__del__ destructor, used when releasing an object.
__repr__ print, convert
__setitem__ assigns value by index
__getitem__ get value by index
__len__ get length
__cmp__ comparison operations
__call__ function calls

__add__ addition operation
__sub__ subtraction
__mul__ multiplication
__div__ division
__mod__ Derivative operations
__pow__ weighs the square
Copy Code The code is as follows.

# Class Definition
    class people: 
# Define basic attributes
        name = '' 
        age = 0 
# Define private properties, which cannot be accessed directly from outside the class.
        __weight = 0 
#Define constructor methods
        def __init__(self,n,a,w): 
            = n 
            = a 
            self.__weight = w 
        def speak(self): 
            print("%s is speaking: I am %d years old" %(,)) 

     
    p = people('tom',10,30) 
    ()

II. Inherited class definitions:
1. Single inheritance
Copy Code The code is as follows.

class <class name>(parent class name)
<statement>.

Copy Code The code is as follows.

class childbook(book)
    age = 10

Copy Code The code is as follows.

#Single Inheritance Example
    class student(people): 
        grade = '' 
        def __init__(self,n,a,w,g): 
# Call the constructor of the parent class
            people.__init__(self,n,a,w) 
            = g 
# Override methods of the parent class
        def speak(self): 
            print("%s is speaking: I am %d years old,and I am in grade %d"%(,,))      

    s = student('ken',20,60,3) 
    ()

2. Multiple inheritance of classes
Copy Code The code is as follows.

class class name(parent1,parent2,.... , parent n)
<Statement 1>.

You need to pay attention to the order of the parent class in the parentheses, if there is the same method name in the parent class and it is not specified when used in the subclass, python searches from left to right, i.e., if the method is not found in the subclass, it will look up from left to right to find out if the parent class contains the method
Copy Code The code is as follows.

#Another class, preparation before multiple inheritance
class speaker(): 
    topic = '' 
    name = '' 
    def __init__(self,n,t): 
        = n 
        = t 
    def speak(self): 
        print("I am %s,I am a speaker!My topic is %s"%(,)) 

#Multiple inheritance
class sample(speaker,student): 
    a ='' 
    def __init__(self,n,a,w,g,t): 
        student.__init__(self,n,a,w,g) 
        speaker.__init__(self,n,t) 

test = sample("Tim",25,80,4,"Python") 
()# method name is the same, by default the method of the parent class is called in the first place in parentheses