Want to know object oriented programming python book? we have a huge selection of object oriented programming python book information on alibabacloud.com
Object-Oriented ProgrammingObject-oriented programming , or OOP: it is a design and programming method to solve the software taking. This method describes the similar logic operation, data and state in the software system in the form of class, which is reused in the software
The core of the process design is the process (pipeline thinking), the process is to solve the problem, process-oriented design is like to design a good line, thoughtful, when to deal with what things.Advantages: Greatly reduce the complexity of the programdisadvantage: A set of pipeline or process is used to solve a problem, the production line of soda can not produce a car, even if it can, also must be big change, change a component, lead a whole b
Website Explanation:
object.__getitem__(Self, key)
Called to implement evaluation ofself[key].for sequence types,The accepted keys should beintegers andSlice Objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) are up to the__getitem__()Method. IfKeyis of a inappropriate type,TypeErrorMay is raised; If of a value outside the set of indexes for the sequence (after any special interpret
1Object-Oriented programmingObject-Oriented Programming:Object-Oriented programming -- Object orientedprogrammingOOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data. pr
blank linemembers=[T,s] forMemberinchMembers:member.tell ()Results:( Initialized SchoolMember:Mrs.Shrividya) (Initialized Teacher:Mrs.Shrividya) (Initialized schoolmember:swaroop) (Initialized Student:swaroop) Name:"Mrs.shrividya"Age:" +"Salary:"30000"Name:"Swaroop"Age:" A"Marks:" the"Note: in order to use inheritance, the name of the base class as a tuple follows the class name when the class is defined. The __init__ method of the base class is specifically called with the self variable, so th
In object-oriented programming, properties and methods are used very frequently. The following is a simple summary of the basic operation of the attribute.
In Python oop, attributes are typically manipulated in the following ways:
1, add attributes
2, modify the value of the property
3, get the value of the property
4,
Self.role=role Self.weapon=Weapon Self.life_value=Life_value Self.money= MoneydefShot (self):Print("%s Shooting ..."%self.name) self.__heart=" die" Print(self.)__heart) defgot_shot (self,):Print("%s Say:ah...,i got shot ..."%self.name)defBuy_gun (Self, gun_name):Print("%s just bought%s"%(self.name,gun_name)) Self.weapon= Gun_name#You can change the properties of an object through a classR1= Role ('Alex','Police','AK47')#generate a roler2 =
0 and ') ... self._sc Ore = value ... >>> s=student () >>> s.set_score >>> s.score Traceback (most recent call Last): File " Attributeerror: ' Student ' object have no attribute ' score ' >>> S.get_score () >>> s.set_score Traceback (most recent call last): File File" Valueerror:value must between 0 and the Note! In this case, there is no way to protect data without an adorner. But with adorners, it's much easier to call a method into a
, methods, and the corresponding variables and methods can be filled in the instance. 5. Class properties and Instance propertiesAn instance can add properties through Bob.name or SetAttr (), and a class can increment properties through in-class code, if the class and instance properties have the same name, the property of the priority invocation instance.#练习题 to count the number of students, you can add a class attribute to the student class, and each time you create an instance, the property a
properties in the instance, it will look up along the inheritance chain. If it is a method, it invokes the nearest method, and if it is a property, it is bound to an attribute on the instance.(3) The polymorphic concept is consistent, any place that can reference the parent class, must be able to use the subclass4. Get related properties after defining class objects(1) type (obj) return variable(2) dir (obj) returns all properties of the variable(3) GetAttr (Obj,attrname) Get Properties: Attrna
binding allows us to dynamically add functionality to class while the program is running, which is difficult to implement in a static language.But what if you really want to restrict the properties of an instance? Only add the name and age attributes to the student instance.For the purpose of limiting, Python allows you to define a special _slots_ variable when defining a class to limit the attributes that the class instance can add:>>classStudent (
1. When the class is used only as a namespace, the following
>> m.a=1Traceback (most recent):File "Nameerror:name ' m ' is not defined>> Class M ():... pass...>> m.x=5>> m.y=8>> m.y+m.x13>>2. Create a class#coding =utf-8
Class Addrbookenter (object): #类定义def Init(self,nm,ph): #定义构造Self.name=nmSelf.phone=ph#print "Createdance for", Self.namedef updatephone (self,newph): #定义方法Self.phone=newphone#print "Update phone for", Self.nameJohn=addr
size!! Can be compared to the same valueFrom enum import enum, unique# @uniqueclass Weekday (enum): Monday = 1 Tusday = 1 wensdday =3 Thursday =9 Fri Day =5print (Weekday.monday = = weekday.wensdday) print (Weekday.tusday is weekday.friday) print (Weekday.tusday > WEEKD Ay.friday)Operation Result:Falsefalsetraceback (most recent): File "/usercode/file.py", line See a good good blog, this talk about the content, he is more concise than Liao. I like it very much. How's My mess? I was purely a not
With open (' path ', ' R ', encoding= ' utf-8 ') as F:code blockThis is called the on-line management protocol, the WITH statement, in order for an object to be compatible with the WITH statement, the __enter__ and __exit__ methods must be declared in the class of the object.Context Management Protocol:Class Open:def __init__ (self,name):Self.name=namedef __enter__ (self):Print (' A With statement appears, the object's __enter__ is triggered, the retu
,score): Self.name=name Self.score=Scorep1= Person ('Bob', 90)#Dynamic binding method for P1P1.get_grade =types. Methodtype (Fn_get_grade,p1,person)PrintP1.get_grade ()#outputs A, the method is dynamically bound to the P1 object.P2 = person ('Alice', 65)PrintP2.get_grade ()#ERROR,P2 doesn't have this method .3. Initializing Instance Propertiesdef __init__ # when an instance is created, it is automatically called Self.attr1_name = attr1 = attr24. Access RestrictionsA property name that star
property to a class, all instances can access the properties of the class, and the class properties accessed by all instances are the same1. Class attribute and instance property name conflictWhen instance properties and class attributes have the same name, the instance property has a high precedence, which masks access to the class properties.2. Class methodsBy marking a @classmethod, the method binds to the person class, not to an instance of the class.Because it is called on the class, not
in all method declarations. This parameter indicates the instance object itself. When you call a method using an instance, an interpreter is quietly passed to the method. Therefore, you do not need to pass self in by yourself, because it is automatically passed in. The general method requires this instance (self), but the static method or class method does not. The class method requires a class instead of an instance.
>>> myObj = MyDataWithMethod()
classMynewobjecttype ():9 'define Myobjecttype New class'TenClass_suiteView CodeWhen it comes to classical and modern classes, it is important to mention the two nouns of depth first and breadth first.The new-type MRO algorithm uses C3 algorithm breadth-first search, while the classic-class MRO algorithm uses depth-first search.Depth first1 #!/usr/bin/python22 #-*-coding:utf-8-*-3 4 classA:5 Pass6 classB:7 Pass8 classC (B):9 PassTen classD (c,a): One PassView CodeThe order o
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.