Python knowledge point 13 --- object oriented programming

Python is a completely object-oriented development language. Everything in it operates in an object-oriented manner

class(Class): A collection of objects that describe the same properties and methods. It defines the properties and methods common to each object in the collection. An object is an instance of a class.
Class variables: class variables are common to the entire instantiated object. Class variables are defined in the class and outside the function body. Class variables are usually not used as instance variables.
Data member: class variable or instance variable, Used to process data related to classes and their instance objects.
Method Rewriting: if the method inherited from the parent class cannot meet the needs of the child class, it can be rewritten. This process is called method overriding( override),Also known as method rewriting.
Local variable: the variable defined in the method, which only acts on the class of the current instance.
Instance variable: in the declaration of a class, attributes are represented by variables. This variable is called an instance variable and is declared inside the class declaration but outside the other member methods of the class.
Inheritance: that is, a derived class( derived class)Inherit base class( base class)Fields and methods. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is such a design: a Dog Objects of type derive from Animal Class, this is simulation"It's a( is-a)"Relationship (example diagram, Dog It's a Animal). 
Instantiation: create an instance of a class and a concrete object of the class.
Method: a function defined in a class.
Object: an instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.

The syntax for defining a class is as follows

class ClassName():
    'Class'   #Class document string
    class_suite  #Class body

	#Class
	def aaa(self) :
		return

Instantiate the class and generate the object. The syntax is as follows

Variable name = Class name()  ---It doesn't need to be like other languages new

The difference between Python classes and other languages is that sometimes there is one more self in the method. When you want to use this variable, it is usually placed in the first parameter bit and represents the object itself. The following example can explain this

class Test:
    def prt(self):
        print(self)
 
t = Test()
print(t)
t.prt()

It is found that all points to the same object:
<__main__.Test instance at 0x10d066878>
<__main__.Test instance at 0x10d066878>

Remember: for self itself, it is not a dead key. We can change it to another name. Just in the development, we write it in the first parameter bit according to the Convention and call it self

Moreover, when you use self, you don't need to care about its value when passing parameters, because it can be automatically supplemented, as shown below

class Test :
	def fun_name(self,b,c):
		print(b+str(c))
		
a = Test()
a.fun_name('ignore self Normal transmission parameters',21)
Result: ignore self Normal transmission reference 21

Python classes also have attributes. We can write attributes inside the class and outside the class entity method, but remember to give an initial value when writing attributes in the class, otherwise an error will be reported when accessing

class Test :
	age = ''
	def fun_test():
		print('llll')

After using this class to create objects, objects will have all the things of the class, and the attributes are naturally included. The syntax of the invocation is as follows. This is called "object attribute acquisition outside the class" in Python.

Object name.Attribute name		#Call get
 Object name.Attribute name = value	#modify

However, note that Python allows an object to have its own attributes. This attribute does not need to be defined in the class. You can directly use object calls to automatically generate a data. This object's own attributes are not available for other objects. The following example can explain this. This is also called "adding object attributes outside the class" in Python

class Test :
	def fun_name():
		print('llll')
		
a = Test()
b = Test()

a.age = '12'
print(a.age)
print(b.age)

The result will report an error and prompt the object b No, age Properties:
12
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    print(b.age)
AttributeError: Test instance has no attribute 'age'

At this time, some young partners may wonder that getting object attributes outside the class and adding object attributes outside the class focus on the object, and the relationship with the class is not very obvious. In fact, python has the last related operation, which is called "getting object attributes inside the class", Personally, I think it is actually an aid provided by Python to our developers to prevent us from accessing the properties of the object due to other factors, such as interference by local variables or other variables. The operation methods are as follows

class Test :
	age = 12
	def fun_name(self):
		print(self.age)
		
a = Test()
a.fun_name()

In fact, it is the application of self, but note that this operation can also take effect in the operation of adding object attributes outside the class

class Test :
	def fun_name(self):
		print(self.age)
		
a = Test()
a.age = 10
a.fun_name()

In fact, for self, it can not only call attributes, but also other things. If you are interested, you can find information in other places, such as the class name of object instantiation

class Test :
	def fun_name(self):
		print(self.__class__)
		
a = Test()
a.fun_name()

result:__main__.Test

Python actually has other ways to call the properties of objects

getattr(obj, name[, default]) : Access the properties of the object.
hasattr(obj,name) : Check if a property exists.
setattr(obj,name,value) : Set a property. If the property does not exist, a new property is created.
delattr(obj, name) : Delete attribute.

hasattr(emp1, 'age')    # Returns True if the 'age' property exists.
getattr(emp1, 'age')    # Returns the value of the 'age' attribute
setattr(emp1, 'age', 8) # Add attribute 'age' with a value of 8
delattr(emp1, 'age')    # Delete attribute 'age'

Python classes themselves actually have properties

Use the class name when calling.Class properties    That's it

__dict__ : Class properties (including a dictionary, which is composed of class data properties)
__doc__ :Class
__name__: Class name
__module__: The module where the class definition is located (the full name of the class is'__main__.className',If the class is in an import module mymod Medium, then className.__module__ be equal to mymod)
__bases__ : Class (including a tuple composed of all parent classes)

Keywords: Python

Added by rishiraj on Thu, 16 Dec 2021 12:27:10 +0200