@property:
The ability to examine parameters and to access variables of the class in such a simple way that a similar attribute can be used allows the caller to write short code
classStudent (object):#Birth is a read-write property (an adorner that defines a setter), and age is a ReadOnly property@propertydefBirth (self):returnSelf._birth @birth. SetterdefBirth (self, value): Self._birth=value @propertydefAge (self):return2014-Self._birth--------------------------------->>>s =Student ()>>>s.birth = >>>S.birth>>>s.age
@classmethod and @staticmethodBoth @classmethod and @staticmethod allow you to access a built-in function directly in a class way
However, the first parameter required by @classmethod is CLS (that is, the class itself, not the instance itself (the instance itself is self))
@classmethod functions can inherit from a class Http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner
classDate (object):def __init__(self, month, day, year): Self.month=month Self.day=Day self.year=Year @staticmethoddefmillenium_1 (month, day):returnDate (month, Day, 2000) @classmethoddefmillenium_2 (CLS, Month, day):#CLS is a object that holds class itself returnCLS (Month, Day, 2000) classDateTime (Date):defdisplay (self):return "{0}-{1}-{2}-00:00:00pm". Format (Self.month, Self.day, self.year) new_year= Date (1, 1, 2013) millenium_new_year_1= Date.millenium_1 (1, 1) millenium_new_year_2= Date.millenium_1 (1, 1) isinstance (new_year, Date)#TrueIsinstance (Millenium_new_year, Date)#Truedatetime1= DateTime (10, 10, 1990) datetime2= Datetime.millenium_1 (10, 10) Datetime3= Datetime.millenium_2 (10, 10) isinstance (datetime1, DateTime)#TrueIsinstance (DateTime2, DateTime)#FalseIsinstance (DateTime2, DateTime)#True
Multiple inheritance
In "1" python, if a subclass has its own constructor, the constructor of the parent class is not automatically called, and if the constructor of the parent class is needed, it needs to be explicitly called in the constructor of the subclass.
"2" If the subclass does not have its own constructor, the constructor is inherited directly from the parent class, which does not have any understanding of the single inheritance (a subclass derives from only one parent).
Issue: If there is multiple inheritance, a subclass derives from more than one parent class, and the subclass does not have its own constructor, the subclass inherits the constructor of the parent class by default.
The "3" subclass derives from more than one parent class, and the subclass does not have its own constructor.
(1) In order to inherit, which parent class is in the front and it has its own constructor, it inherits its constructor;
(2) If the first parent class does not have a constructor, the 2nd constructor is inherited, the 2nd one is not, and then it is searched backwards, and so on.
Mixin profile is similar to multiple inheritance (in fact, you can think of Mixin as an application of multiple inheritance in a particular scenario),
However, the classes and Mixin classes that are usually mixed with Mixin are not is-a, and the Mixin classes are mixed to add some (optional) functionality.
The freedom to blend in with the Mixin class gives you the flexibility to add different functions to the class being mixed.
class Dog (mammal, Runnablemixin, carnivorousmixin): Pass
Custom Classes
Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013946328809098c1be08a2c7e4319bd60269f62be04fa000
Https://docs.python.org/2/reference/datamodel.html#special-method-names
1. __str__, __repr__
classStudent (object):def __init__(self, name): Self.name=namedef __str__(self):return 'Student Object (name=%s)'%Self.name__repr__=__str__>>>PrintStudent ('David')>>>student ('David')----------------Student Object (name=david)#__str__Student Object (Name=david)#__repr__
2 __getattr__
classStudent (object):def __init__(self, name): Self.name='Michael' def __getattr__(self, attr):ifattr=='score': return99>>>student = Student ('David')>>>Student.name>>>Student.score>>>PrintStudent.grade---------------'David'99None#you can have class respond only to a specific number of properties (by throwing Attributeerror errors)
3 __iter__
If a class wants to be used for the for ... in loop, like a list or tuple, you must 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#Initialize two counters A, B def __iter__(self):returnSelf#The instance itself is an iterative object, so it returns itself defNext (self): SELF.A, self.b= self.b, SELF.A + self.b#Calculate Next Value ifSELF.A > 1000:#conditions for exiting a loop Raisestopiteration (); returnSelf.a#returns the next value>>> forNinchFib (): ...PrintN,------------------------1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
4 __getitem__
class Fib (object): def __getitem__ (self, N): = 1, 1 for in Range (n): = B, A + b return a ------------------------->>> f = Fib ()>>> f[0]1>>> f[1] 1>>> f[2]2>>> f[10]89>>> f[100]573147844013817084101
5 __call__
And with the callable () function, we can tell if an object is a callable object
classStudent (object):def __init__(self, name): Self.name=namedef __call__(self):Print('My name is%s.'%self.name)>>>student = Student ('DAIVD')>>>student ()---------------------My name isDavid.
2015-05-09
Python's object-oriented advanced programming