Python Learning Notes (eight) object-oriented extension

Source: Internet
Author: User

Original chain: http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html

NOTES: Today's content feels like it's not easy to understand

1 #when I learned to define my move yesterday, I used self.2 " "3 def Bird (object):4 Have_feather = True5 way_of_reproduction = ' egg '6 7 summer = Bird8 print (summer.way_of_reproduction)9 " "Ten #There's an error here. One #attributeerror: ' function ' object has no attribute ' way_of_reproduction ' A #The define used above, the class that was used yesterday, the above are annotated, the following re - -  - classBird (object): theHave_feather =True -Way_of_reproduction ='Egg' -  -Summer =Bird + Print(summer.way_of_reproduction) -  + #print is egg, normal, next step. A #and then the summer into a movable chicken. at " " - class Bird (object): - Have_feather = True - way_of_reproduction = ' egg ' - def move (self, x, y): - position = [0, 0] in position[0] = position[0] + x - position[1] = position[1] + y to return position +  - summer = Bird the Print (Summer (5,8)) * " " $ #again the error ... Typeerror:object () takes no parametersPanax Notoginseng #Forget summer.move. - #It also shows that the def move has become a bird attribute . the " " + class Bird (object): A Have_feather = True the way_of_reproduction = ' egg ' + def move (self, x, y): - position = [0, 0] $ position[0] = position[0] + x $ position[1] = position[1] + y - return position -  the summer = Bird - Print (Summer.move (5, 8))Wuyi #TypeError: Move () Missing 1 required positional argument: ' Y ' the # .... Don't know what to say, yesterday all normal printing ah - " " Wu  - classBird (object): AboutHave_feather =True $Way_of_reproduction ='Egg' -     defMove (self, x, y): -Position =[0, 0] -Position[0] = position[0] +x APOSITION[1] = position[1] +y +         returnposition the  -Summer =Bird () $ Print(Summer.move (5, 8)) the #Bird behind the short brackets .....  the #here is the introduction of self the  the #written by: - #Self represents an object. Object has all the properties of a class, then we can invoke the Class property through self in  the " " the class Human (object): About laugh = ' hahahaha ' the def show_laugh (self): the print (Self.laugh) the  + #因为我们上面发现, Def Move,summer.move can invoke the properties of the move - #那这里, the author used Self.laugh to do a test . the Li_lei = Human ()Bayi print (Li_lei.laugh) the #输出结果: Hahahaha the #然后print (Li_lei.show_laugh) - #然后就 ... It's crazy. - #输出: <bound method Human.show_laugh of <__main__.human object at 0x000002a28d83ec50>> the #不懂, keep reading the author . the " " the  the classHuman (object): -Laugh ='Hahahaha' the     defShow_laugh (self): the         Print(Self.laugh) the     deflaugh_100th (self):94          forIinchRange (100): the Self.show_laugh () the  theLi_lei =Human ()98 li_lei.laugh_100th () About  - #This is the author's101 #print (li_lei.laugh) #输出是hahahaha102 #Direct Li_lei.laugh #输出是 ' hahahaha ', is the string? 103 #It was later understood that the summer.way_of_reproduction output was ' egg '104  the #print (Li_lei.show_laugh ()) #输出是hahahaha None106 #Li_lei.show_laugh () #输出是hahahaha107 108 #print (li_lei.laugh_100th ()) #输出是100个 hahahaha None109 #li_lei.laugh_100th () #输出是100个 hahahaha the 111 #There is no understanding .... Why? What is None?  the 113 #laugh is a property of the human class, and self is an object that has all the properties of the class the #Self is the property of human . the #defines the Show_laugh function, in which self uses the laugh attribute the #laugh_100th with Show_laugh function, Show_laugh function is Self.laugh, that is human laugh117 118 #I don't know if that's the right thing.119  - 121 122 123 124 #Special Methods Special method the classHappybird (bird):#Happybird inheriting bird properties126     def __init__(self, more_words):127         Print('We are happy birds.', More_words) - 129Summer = Happybird ('Happy, happy!') the #do not get init int ha, error .... 131 #automatic invocation, process called initialization the #Execute summer.__init__ (more_words), put happy, happy handed to More_words133 #__init__ () automatically executes when an object is created

Legacy: The nature of an object

Python Learning Notes (eight) object-oriented extension

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.