Want to know object oriented programming python book? we have a huge selection of object oriented programming python book information on alibabacloud.com
(self): print (' Dog eatting ') def bark (self): print (' Dog Bark ') Dog=dog () Cat=cat () cat.eat () def eat (obj): Obj.bark () Eat (CAT) Duck typePython advocates duck type, that is, ' if it looks like, sounds like and walks like a duck, then it's a duck 'Python programmers usually write programs based on this behavior. For example, if you want to write a custom version of an existing obje
) self.salary=salaryThis is actually the same as the inheritance of the wording, if the parent class name changed, in the subclass also to change, more elegant style is to use super ()class Parent (object): def __init__ (self,name,age): self.name=name self.age=AgeClass child ( Parent): def__init__(self,name,age,salary): super (). __init__ (name,age) self.salary=salaryThis is the notation in Python3, if it is w
extended the flexibility and maintainability of programs and is widely used in large project design. In addition, supporters claim that object-oriented programming is easier to learn than previous practices because it allows people to design and maintain programs more easily, making them easier to analyze, design, and understand. Opponents have denied this in so
changes the passed in parameter to the variable inside the class Self.nose= 2Self.face= 2Self.head= 10self.wing= 4Print('Start Testing') defdriver (self):Print('Test it.') self.eat ()#calling functions inside a class defFly (self):Print('%s can fly'% self.name)#get the variables inside the class defEat (self):Print('%s eat hot pot, cake'% self.name)Class is passed into the parameter, the parameter is changed into a variable inside the class, and then other places within the class can b
doesn't have the keyword "new" at all). Instead, when Python creates an instance, it calls the \\init\\_ () method during instantiation, and when a class is instantiated, it can define additional behavior, such as setting an initial value or running some preliminary diagnostic code--primarily after the instance has been created, before the instantiation call returns to the instance. To perform certain tasks or settings.
Create a class (class definiti
classCitizen:4 def __init__ (self):5Citizen. idcard='515611'6Citizen.name='Naruto'7Citizen.age= -8 def KK (self):9 Print (citizen. Idcard,citizen.name,citizen.age)Ten One classStudent (citizen): A def __init__ (self): -Student.number='123123' -Student.room='153' theStudent.age= the - def KK (self): - #如何输出以前的值? Still using citizen? Or with a student? It seems to be all. - Print (citizen. Idcard,citizen.name,citizen.age,student.number,student.room,student.age) + -# c=Citizen () + # C.KK () AD
1. Access RestrictionsIf you want the internal properties to be inaccessible externally, you can add the name of the property with two underscores __ , and in Python, the variable name of the instance __ becomes a private variable (private), which can only be accessed internally and cannot be accessed externally, so We changed the Student class:Class Student (object): def __init__ (self, Name, score):
in class, so it is an error.Look again, the class with __getattr__:#!/usr/bin/python#-*-Coding:utf-8-*-Class Student (object):def __init__ (self):Self.name = ' Michael 'def __getattr__ (self, Other):If other== ' score ':Return 99s = Student ()Print S.namePrint S.score #Class中没有这个属性Print S.gg #Class中没有这个属性Look again, print's score and GG are not defined in class, but both have outputs. Because the program t
(CLS,): #cls?Return student.get_student_total@classmethodDef get_student_list (CLS,): #cls?Return student.get_student_list@staticmethodDef static1 ():Return ' This is a static method form Student,it are can be called by both the class and the instance 'Xiaoming=student (' xiaoming ', ' Male ')Xiaoming=student (' ximing ', ' Male ')Student.get_student_list ()#有问题, did not achieve the desired effectstatic method (Normal method):Use the @staticmethod adorner when definingStatic methods have no par
#add deleter for attribute score@score. Deleterdefscore (self):Print('You delete the score!') delSELF._SCORESTU1=Student ()Print('-'* 50) Stu1.score= 88Print('-'* 50)Print(Stu1.score)Print('-'* 50) Stu1.score= 92Print(Stu1.score)delStu1.scoreExecution Result: --------------------------------------------------youset the score! --------------------------------------------------youget the score! --------------------------------------------------youset the score!you get the score! Delete the
a constructor. You didn't call "new" to create a new object. (Python has no "new" keyword at all). Instead, after Python creates the instance, in the instantiation process, the \\init\\_ () method is invoked, and when a class is instantiated, additional behavior can be defined, such as setting the initial value or running some preliminary diagnostic code-primari
= Student ()>>> S.age>>> S.age ()24called only if no attribute is found __getattr__ , existing attributes, such as name, are not found in __getattr__ . >>> class Student (object):... def __getattr__ (self, attr):... if attr = = ' age ':... return lambda:24... raise attributeerror (' \ ' student\ ' object has no attribute \ '%s\ '%attr)...>>>>>> s = Student ()>>> S.nameTraceback (most recent):File "File "A
Object-oriented: Classes and objects, classes are abstract, and are concepts. objects are real things. A class is a template for an object that is created by a class.The class consists of three parts, 1. Class name. 2. Properties. 3. Methodsclass Cat:def Eat (self):print ("Cat eats fish ...")def run (self):print ("The cat is Running")def introduce (self):print ("
implement a __iter__ () method that returns an iterative object, and then the python for loop constantly calls the next () of the Iteration object. The Stopiteration method gets the next value of the loop until it exits the loop when it encounters an error.classFib (object):def __init__(self): SELF.A, self.b= 0, 1#Ini
: ' Myredis ' object has no attribute ' __conn_redis ' - Print(R.get ('Kate11')) theMyredis.other ()#static methods do not need to be instantiated *Myredis.class_fun ()V. InheritanceIn OOP programming, when we define a class, we can inherit from an existing class, and the new class is called a subclass (subclass), and the inherited class is called the base class, the parent class, or the superclass (base-Cl
The content of this article
Interface and Normalization design
Polymorphism and polymorphism
Packaging
Object-oriented Advanced
Exception handling
Network programming
First, interface and normalization of design1. Definition(1) Normalization allows the user not to care about the object's class, just to know that these objects hav
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.