zoukankan      html  css  js  c++  java
  • PYTHON_DACORATOR

    装饰器在后面(异步io)威力强大

    作用:在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator)。

    本质上,decorator就是一个返回函数的高阶函数(传入将要装饰函数,返回函数体函数):

      def 函数名(参数是函数):

        函数体

        return 返回值是函数体函数

    example:

     1 #装饰器:
     2 def log(func):      #传入参数函数(对函数func装饰)
     3     def wrapper(*args,**kw):
     4         print('call %s():'%func.__name__)
     5         return func(*args,**kw)      #装饰完成,返回函数func
     6     return wrapper
     7 
     8 #调用装饰器:
     9 @log
    10 def now():
    11     print('2017-2-25')
    12 
    13 #装饰完成函数:
    14 now() 

     @property

    Python内置的@property装饰器就是负责把一个方法变成属性调用

    可对class属性做设置,有getter和setter方法

     1 class Student(object):
     2 
     3     @property
     4     def birth(self):
     5         return self._birth
     6     @birth.setter
     7     def birth(self,value):
     8         self._birth = value
     9 
    10     @property
    11     def age(self):      #设置age属性,限制age属性
    12         return 2015 - self._birth

     

  • 相关阅读:
    ARC071F Infinite Sequence
    AGC043C Giant Graph
    ARC006E Addition and Subtraction Hard
    Codechef BALNET Balancing Network Revisited
    Gym102055H Game on the Tree
    Luogu P5320 [BJOI2019]勘破神机
    cookie和session
    jsp介绍
    request请求转换成对象。
    域对象 request
  • 原文地址:https://www.cnblogs.com/wilson297/p/6441352.html
Copyright © 2011-2022 走看看