Chapter Content
- Introduction to Programming Paradigms
- Classes and instances
- Properties and Methods
- constructor function
- Name space
- Relationship of attributes (static and dynamic) to a class
- Combination of object interaction and class
first, the programming paradigm
Programming is a programmer with a specific syntax + data structure + algorithm composed of code to tell the computer how to perform the task of the process, and the way to achieve a task there are many different ways, the characteristics of these different programming methods summed up the programmatic category, that is, the programming paradigm
Process-oriented programming is the program from the top to the next step, the basic design idea is that the program is to start to solve a big problem, and then a big problem into a lot of small problems or sub-process, the process of writing sub-process to continue to decompose until the small problem is simple enough to be solved in a small step range
In Python, we can break down complex tasks into simple tasks by splitting large pieces of code into functions and using a layer of function calls, and this decomposition can be called process-oriented programming. function is the basic unit of process-oriented program design
Functional programming is a very high degree of abstraction of the programming paradigm, the purely functional programming language functions are not variables, a function of programming is to allow the function itself as a parameter to another function, but also allow the return of a function, Python for functional programming to provide partial support. Because Python allows the use of variables, Python is not a purely functional programming language
Object-oriented programming is the use of "classes" and "objects" to create a variety of models to achieve a real-world description, the use of object-oriented programming because it can make the maintenance and expansion of the program easier, and can greatly improve the efficiency of program development, in addition, Object-oriented programs can make it easier for people to understand your code logic, making team development easier
second, class and example
Syntax of the class
Class Name: Pass
A chestnut??
# Create a person's ' class ', the first letter to capitalize class person (object): # constructor, initialize property def __init__ (self,name): self.name = name # People can eat def eat (self): print ("I am Eatting") # created a man called ' Lyon ' p = person (' Lyon ') # Perform meal function p.eat () # Execution Result: I a M eatting
Class is对现实生活中一类具有共同特征事物的抽象
A class acts as a template, and when we create a class, it is equivalent to creating an initial ' model ' that we can use to create something with the same features or functions to help us better handle the problem.
After the above pest class name person has one (object)
, this is the style of the new class, and in the version above python3.x, the default is the new class, so you can also directlyclass Person:
When we create a class, we inherit the object class by default, and the object is described later in this article
We know that the class is an abstraction, and since it is abstract it is not operational, so if we do it we need to turn this abstract concept into something concrete, a process we call instantiation
Instantiation:由抽象的类转换成实际存在的对象的过程
Example: The 由类进行实例化所得到的对象
above Pest p
is an example
third, property and method
A property is a descriptive property or characteristic of an entity, such as a person's name, age, gender, etc. Of course there are people who can do things that are also a property, such as eating, sleeping, drinking water and so on. For both of these properties, one is a characteristic, called a static property, and the other is a function, called a dynamic property.
In Python, we call a static property, which is 属性
called a dynamic property, 方法
and a variable to represent the property, as a function representation method, see:
PS: A function in a class is no longer called a function, but is called a method
Properties include: instance variables, class variables
Invocation mode: Class name. Property name
Class Person: # classes variable role = ' student ' # constructor def __init__ (self,name): # instance variable self.name = Name
Methods include: Common methods, attribute methods, class methods, static methods
Invocation mode: Class name. Method Name ()
Class Person: # normal method Def eat (self): Pass
Special class Properties
Property name |
Description |
__dict__ |
To view a class or object member, return a dictionary |
__name__ |
View the name of the class |
__doc__ |
View the description of the class, that is, the comments section |
__base__ |
View first Parent class |
__bases__ |
View all parent classes, returning a tuple |
__module__ |
View the current module of the class |
__class__ |
View objects by what kind of instantiation |
PS: For properties and methods, in the online classification of a variety of all, such as fields, as well as some of the rookie tutorial, in fact, is essentially a thing
Four, the constructor function
In the above example, you can see that there is a __init__ method, which is called the constructor method, which is used to initialize the property, so if we want to set the property, then the construction method is required
Self
We use examples to illustrate
Class Foo: def __init__ (self,name): self.name = name def func (self): print (ID (self)) a = Foo (' Lyon ') # Print instance A's memory address print (ID (a)) # Call the Func method in the class, that is, print Self's memory address A.func () "' Execution result: 1703689404544 1703689404544 result Analysis: We find that the memory address of a is the same as the memory address of self, that is the instance itself, so when we instantiate it, Self.name = name is adding a Name property to the instance, and the value of the property is the ' Lyon ' that we passed in when we instantiated it. So if we need to add an attribute to an object, we can add it directly through the object. Property Name = property value.
Take the constructor in the Shangli and look at it in a different pose.
A = foo (' Lyon ') # is equivalent to calling a method in a class using the class name foo.__init__ (A, ' Lyon ') # The Python interpreter will automatically trigger the __init__ method, so the following Foo (A, ' Lyon ')
Five, the name space
In the function, when the Python interpreter executes, the function name is loaded sequentially into the namespace, and the class of course is the same
When we create a class, the Python interpreter creates a namespace for the class that is used to store all the names (properties and methods) defined in the class, and when we instantiate it, the Python interpreter creates an instance namespace for us to hold the names in the instance
When we use 类名. 名称
to access object properties (static vs. dynamic), the Python interpreter first finds the name in the namespace of the object, finds it in the namespace of the class (the class before it instantiates the object), and then throws an exception if none is found.
accessing attribute instances
Class A (object): "" " This is a class " "" pass a = A () # Access the __doc__ property of instance a print (a.__doc__) #执行结果: This is a class
Explanation: For instance a itself there is certainly no __doc__ attribute, which is beyond doubt, because we simply do not use constructors to increase instance properties. According to the results of the execution, we have access to the __doc__ attribute in this class, then you would say that this class also did not see the __doc__ attribute Ah, actually class A is some, because it inherits the object class, as to what is the object class, what is there? Read the next article.
vi. attribute (static and dynamic) relationships to classes
Because Python is a dynamic language, the assignment mechanism of Python is implemented by dynamic binding.
- Class properties are shared to all objects
First instance after the description
Class Foo: # Defines a class variable, deliberately using the container type to illustrate name = [' Lyon '] # instantiation a = foo () b = foo () # access to the Name property in a, a (' Name property in instance a: ', a . Name) print (' Name property in instance B: ', b.name) # View the memory address of the Name property in A,b,foo print (ID (a.name)) print (ID (b.name)) print (ID (foo.name)) Print ('------------------') # Modify the class variable foo.name = ' A ' # to access the Name property in the A, B again, print (' Name property in instance a: ', a.name) print (' Name property in ' instance ': ', B.name) # Modify the Name property in a? No, it is new a.name = [' Lyon '] # See the memory address of the Name property in a, b again (ID (a.name)) print (ID (b.name)) "Execution Result: The name attribute in instance a: [' Lyon '] Nam in instance b E property: [' Lyon '] 2247471754696 2247471754696 2247471754696------------------The name attribute in instance a: The name attribute in instance B: a 2247471792392 22 47471754696 ""
Description
Deliberately use the container type for experimentation because there is only one reason for the container type memory address in Python, which is that the same object
When we first look at the memory address, the memory address of the name attribute in a, B, Foo is the same, the instance can be 实例.类变量名
accessed in the same way, and all instances share the Class property name
a.name = [‘Lyon‘]
This step does not actually modify the Name property in a, knowing that the Name property is not an instance of the class, and that performing this step will add a new name property with the same name to instance a, since the assignment binding will destroy the channel that originally accessed the class property name, but does not affect the B access to the Class property name
First instance after the description
Class Foo: def func (self): pass a = foo () b = foo () # Prints the memory address of Func in a, B, print (A.func) print (B.FUNC) # ID Returns the inside of a 10 binary representation Save address, converted to 16 binary print (Hex (ID (a))) print (Hex (ID (b))) ' Execution Result: <bound method Foo.func of <__main__. Foo object at 0x000001a7d2f74080>> <bound method Foo.func of <__main__. Foo object at 0x000001a7d3759898>> 0x1a7d2f74080 0x1a7d3759898 "
Description
There is a mapping between the method name and the memory address, and by executing the result we can find that the memory address of the A.func is the same as the memory address of a, then the Func is bound to a
A.func and B.func memory address is not the same, because each instance has opened up its own memory space, Func bound into the location of the natural different
We create an instance of the class by creating a new memory space in memory to hold all the properties of the instance, and once the instance attributes are created, there is nothing much to relate to the class. If you want to modify the instance properties then it can only be modified by the instance, and the instance and the instance are non-interfering.
As in, classes and instances, instances and instances all open up their own memory space
Vii. combination of object interaction and class
Object interaction
Class Person: def __init__ (self, name): self.name = name def attack (self,per): print ("{} attacked {}". Format (Self.name, per.name)) Lyon = person ("Lyon") Kenneth = Person ("Kenneth") Lyon.attack (Kenneth) # execution result: Lyon attacked Kenneth
Combination of Classes
The combination of the reference time
Class BirthDate: def __init__ (self, year, month, day): self.year = year self.month = month Self.day = Day Class Person: def __init__ (self, Name, birthdate): self.name = name self.birthdate = Birthdate p = person (' Ly On ', BirthDate (2000, 1, 1))
When defining a combination
Class BirthDate: def __init__ (self, year, month, day): self.year = year self.month = month Self.day = d Ay class Person: def __init__ (self, name, year, month, day): self.name = name self.birthdate = Birthdate (y Ear, month, day) p = person (' Lyon ', 2000, 1, 1)
The path of Python-object-oriented first knowledge