adorners (adorners are functions used to decorate functions)
Normal functions:
def function name (formal parameter): # variable name
function body return
variable | constant
print function name (argument)
Decorative Device:
def function name 1 (formal parameter): #此形参传递函数
def function name 2 (formal parameter):
function body return
variable | constant return function
name # #也就是函数2的地址
There are two ways to call an adorner
1.@ Function Name 1 #此方法作用在要使用的函数之上
2. function = function Name 1 (function) #此方法与上相同, different ways, it is recommended to use the above
Example:
def dtimer (fun):
def timer (*args): # Higher order function #args = (1,2,3,4)
start_time = Time.time () Fun
(* args) # args = (1,2,3,4), unpack * (1,2,3,4)
stop_time = Time.time () return
"run%s"% (stop_time-start_time)
return Timer # Returns the address of a timer that requires timer ()
@dtimer # fun1 = Dtimer (fun1)
def fun1 (*args) to perform this function:
print "In the fun1 ..."
print args
time.sleep (1)
Anonymous Functions
Lambda parameter: return value
Used primarily in higher-order function call functions to reduce the amount of code, without defining the function name of object-oriented programming
Process oriented: All things have to be done personally.
Object oriented: Create an object, all things to the object to complete
Some key nouns for object-oriented
The drawings of ===== building houses (three rooms, one hall, two rooms, one hall ...)
Object ===== actually built house (number)
Instantiate = = = Build process (the process of creating an object based on a class)
class people (object): # class
def __init__ (self,name,age): # constructor
self.name = name # attribute
self.age = Age
def eat (self): #方法
print "%s is eating spicy bars ..."% (self.name)
def echo_self (self):
print Self
Tianfeng = People ("Tian Feng",) # The object
print Tianfeng
tianfeng.echo_self () # that is instantiated from the class #通过对象来调用方法
The self system creates objects that we instantiate, just the address of this object
Two ways to Access object properties:
1.self.name
2.tianfeng.name Object-oriented three features: encapsulation, inheritance, polymorphism
Encapsulation: All operations are packaged into a whole that is not seen by the outside world, only the functions provided by the opening part
Inheritance: A class can inherit another class, including all variables, built-in functions ...
Polymorphism: In the premise of inheritance, you can write the relevant functions to replace the inherited functions, to maintain the independence of the subclass
Multiple inheritance
-Python classes can inherit more than one, Java and C # can only be inherited;
-Inherit multiple classes, there are two algorithms: depth-first algorithm and breadth-first algorithm;
-Class C: When multiple inheritance uses depth-first algorithm;
-Class C (C1,C2), when multiple inheritance uses breadth-first algorithm;
Eg:d, C (d), B (d), A (B,C)
-Depth First: a->b->d->c
-Breadth First: The private property of the A->b->c->d class
When there are properties in our class that we do not want to be accessed and modified by others,
We can turn this property into a private property
-Create by: two underline before attribute
class people (object):
def __init__ (self, Name, money):
self.name = name
Self.__money = Money #此为私有属性
Private property cannot be accessed
The essence of private property: A method within Python to rename Self._money to its class name __money class
There are situations where we cannot create objects of the class, but we also need to invoke the method of the class.
At this point, you need to create a method of the class, which can be invoked using the class name without instantiating the
-Create by: @classmethod The method keyword for the tag class, and the return value is an object
@classmethod #类的方法关键字
def from_str (cls,s): # class, the first parameter of the class method is the class itself, the CLS = Date year
, month, day = map (int , S.split ("-"))
d = CLS (Year,month,day) # d = Date (year,month,day) return
D # object
d1 = Date.from_str ( ' 2018-10-19 ') #调用
static method of class
Static methods can also be invoked directly using the class name
-How to create: @staticmethod the static method keyword of the tag class
@staticmethod
def is_date_legal (s): # static method, the first argument is neither an object nor the class itself, year
, month, day = map (int, s.split ("-") ) return the year
>= 1970 and 0 < month <= and 0 < day <= 31
private property of class
-Class properties, which only need to be stored once in memory;
-a property in a constructor that needs to be stored once per instantiation of an object;
class people (object):
country = " #私有属性
def __init__ (self, name): #构造函数中的属性
Self.name = Name