Day7 object-oriented advanced, day7 object-oriented advanced

Source: Internet
Author: User

Day7 object-oriented advanced, day7 object-oriented advanced

Advanced object-oriented syntax

Pass@ StaticmethodThe decorator can change its decoration method intoStatic MethodWhat is a static method? In fact, it is not hard to understand that a common method can be called directly after instantiation, and self can be used in the method. call instance variables or class variables, but the static method does not allow access to instance variables or class variables. A method that does not allow access to instance variables and class variables, in fact, it has nothing to do with the class itself. The unique association between the class and the class is to call this method through the class name.

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ staticmethod def talk (): print ("I like to study python") class Teacher (SchoolMember): def _ init _ (self, name, age, sex, course, salary): super (Teacher, self ). _ init _ (name, age, sex) self. course = course self. salary = salary def Teaching (self): print ("Teacher % s is teaching % s. "% (self. name, self. course) s1 = Teacher ("alex", 22, "Femal", "python", 10000) print ("before:", s1.member _ nums) SchoolMember. member_nums = 12 print ("before:", s1.member _ nums) s1.member _ nums = 666 # generate a variable print ("after:", s1.member _ nums) in the class) schoolMember. member_nums = 12 print ("after:", s1.member _ nums)

In the code above, member_nums is a class variable. If s1.member _ nums is called directly, the value in the class is called. If s1.member _ nums = 666, it is equal to adding a new variable to the instance. When modifying the class value, the variable value in the instance will not be affected. The output result of the above Code is as follows:

Before: 0
Before: 12
After: 666
After: 666

Static Method of the class @ staticmethon:

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % SchoolMember. member_nums) @ staticmethod def walk (self): print ("% s is walking ...... "% self. name) # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.walk ()
The running result is as follows:
Traceback (most recent call last ):
File "/home/zhuzhu/day7/staticmethon method. py", line 22, in <module>
S1.walk ()
TypeError: walk () missing 1 required positional argument: 'Self'

In the above Code, if there is no @ staticmethon, the code execution is certainly normal, but when @ staticmethod is available, the system prompts that a parameter is missing. If we change a method to a static method, this method has little to do with the instance.

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % SchoolMember. member_nums) @ staticmethod # strip the method from the class and it has nothing to do with the class. To call this method, the def walk (self): print ("% s is walking ...... "% self) # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.walk ("alex ")

@ Staticmethod: the static method does not associate the methods in the class with the class. During the call, you must pass parameters before calling.

Class Method

Class Method@ ClassmethodDecorator implementation. The difference between a class method and a common method is that a class method can only be a class variable and cannot access instance variables.

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) # @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % self. name) SchoolMember. member_nums # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.talk ()

In the code above,(1) the class cannot directly access the attributes in the instance.; (2)@ ClassmethodThe role of the program is to allow the program to only variables in the category class, such as SchoolMember in the above Code. member_nums, which is a class method, which can be accessed in talk, but cannot be accessed by self. name, because @ classmethod can only be a category attribute.

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % self. name) SchoolMember. member_nums # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.talk () the running result is as follows: Traceback (most recent call last): File "/home/zhuzhu/day7/staticmethon method. py ", line 18, in <module> s1.talk () File"/home/zhuzhu/day7/staticmethon method. py ", line 13, in talk print (" % s like to study python "% self. name) AttributeError: type object 'schoolmember 'has no attribute 'name'

The above code @ classmethon prohibits instance variables in the class and can only use class variables. That is, you cannot use self. name, self. age, and self. sex. You can only use variables of the self. nember_nums and SchoolMember. member_nums classes. As follows:

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % SchoolMember. member_nums) SchoolMember. member_nums # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.talk () the running result is as follows: 0 like to study python

Attribute Method

Attribute methods are used to convert a method into a static property through @ property.

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % SchoolMember. member_nums) @ property # converts a class method into an attribute. To call a method, use s1.walk without parentheses. This is called an attribute def walk (self ): print ("% s is walking ...... "% self. name) # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.walk ()
The running result is as follows:
Traceback (most recent call last ):
File "/home/zhuzhu/day7/staticmethon method. py", line 22, in <module>
S1.walk ()
TypeError: 'nonetype 'object is not callable

If @ property is not added, the program runs normally. But after @ property is added, the program runs incorrectly. Why? Because @ property changes the method of the class to the attribute of the class, we only need to execute s1.walk () during the call without adding parentheses, as shown below:

Class SchoolMember (object): def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex member_nums = 0 def introduce (self): print ("My name is % s, and I am % s year old. "% (self. name, self. age) @ classmethod # class method, cannot access instance variable def talk (self): print ("% s like to study python" % SchoolMember. member_nums) @ property # converts a class method into an attribute. To call a method, use s1.walk without parentheses. This is called an attribute def walk (self ): print ("% s is walking ...... "% self. name) # SchoolMember. talk () # It cannot be called. The class cannot access instance variables. It can only access itself s1 = SchoolMember ("Alex", 22, "Female") # instantiate s1.walk
The running result is as follows:
Alex is walking ......

In the code above, @ property changes the class method to the member's property. We can directly use s1.walk to call it.

Classic vs new

Class A: # the writing of the classic class. The new class is A (object). Try to use the classic class as little as possible, and use the new class. Now def _ init _ (self, name): self. name = name def f1 (self): print ("f1, ") class B (A): def _ init _ (self, name): super (B, self ). _ init _ (name) # def f1 (self): # print ("f1, ") class C (A): def _ init _ (self, name): super (C, self ). _ init _ (name) # def f1 (self): # print ("f1, join us! ") Class D (B, C): passd = D (" Alex ") d. f1 ()

 

In the code above, Class D inherits class B and class C. When we execute the method in Class D, we first search in Class B, this classic class is the same as the new class. If it cannot be found, the classic class is searched in Class A, and the new class is searched in Class C. The example is as follows: (Note: must go 2. there are differences between running in Version X, 3. X is optimized. If no search is found in Class B, search in Class C)

The execution sequence of classic classes and new classes is as follows:

(New class) First executes the class at the same level

(2) Classic class (first execute the class at the upper level)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.