Not much to say, given directly to learn the code recording
# Python control of property rights is achieved by property name # If a property from the beginning of the double underscore (__), the property can not be accessed outside the property # 1 beginning with a double underscore example can not be directly accessed outside class the Person (object): # obtain new object class inherits class DEF the __init__ ( Self , name): Self .name name = Self ._sex = 'M' Self .__ Age = 20 is P = the Person ( 'Alice' ) print(p.name) print(p._sex) # Print (p .__ age) # this place will complain and tell you that there is no property Print (the p-. __Dict__ ) # can see other attributes have not changed, but __age became _Person__age Print (p._Person__age) # this allows access to the property __age # If a property to "__xx__" form definition, it can be accessed by external # because to "__xx__" defined attributes in python called special property, there are many pre You can use special attributes defined # property to begin with a single underscore although it may be external access, but by convention should not do # use types.MethodType () to a function becomes a method Import types DEF fn_get_grade (Self): IF self.score> = 80 : return 'A' IF self.score> = 60 : return 'B' return 'C' class Person(object): def __init__(self, name, score): self.name = name self.score = score p1 = Person('Bob', 90) p1.get_grade = types.MethodType(fn_get_grade, p1) print(p1.get_grade()) the Person = P2 ( 'Hansen' , 75 ) # Print (p2.get_grade ()) # will complain and tell you attribute does not exist # and properties similar method can be divided instance methods and class methods class the Person (Object): = COUNT 0 @classmethod # By this marking, the method to bind to the class, the class of example, and not, as is usually the name of the parameter named CLS DEF how_many ( CLS ): return CLS .count the @Property # call methods like the same access properties DEF getName ( Self ): return Self .name def __init__(self, name): self.name = name Person.count = Person.count + 1 print(Person.how_many()) p1 = Person('Tim') print(Person.how_many()) print(p1.getName) # Because it is in the class calls rather than calling on the instance, therefore Class methods can not get any instance variables, can only get class reference # class inheritance # polymorphism: the same methods and processes have different feedback class the Person (Object) : DEF the __init__ ( Self , name , Gender): Self .name name = Self .gender = Gender class Student (the Person): DEF the __init__ ( Self , name , Gender , Score): super(Student, self).__init__(name, gender) # 继承Person的name和gender self.score = score class Teacher(Person): def __init__(self, name, gender, course): super(Teacher, self).__init__(name,gender) self.course = course p = Person('Bob', 'Male') s = Student('Alice', 'Female', 90) t = Teacher('David', 'Male', 'Math') the isinstance (p , the Person) # p type determines the isinstance (S , the Person) the dir (S) # acquisition variable all attributes getattr (S , 'name' ) # get the name attribute, if the attribute does not exist error getattr (S , 'name' , 'Bob' ) # get the name attribute, if the property is not present the default value is returned 'Bob' setattr (S , 'name' , 'Hansen' ) # Comparison operators # __cmp __ (Self, OTHER) # __eq __ (Self, OTHER) # __lt __ (Self, OTHER) # __gt __ (Self, OTHER) # numerical operators # __add __ (Self, OTHER) # __sub __ (Self, OTHER) Save # # __mul __ (Self, OTHER) # __div __ (Self, OTHER) except # # logical operator # __or __ (Self, OTHER) # __and __ (Self, OTHER) # Switch string # __str__ # __repr__ # __unicode__