2018/05/23python built-in @property decorator
[@property] (Https://www.programiz.com/python-programming/property)
[Decorator] (Https://wiki.python.org/moin/PythonDecorators#What_is_a_Decorator)
[Pythondecoratorlibrary] (https://wiki.python.org/moin/PythonDecoratorLibrary)
[Foofish-python's Zen: Understanding the Python adorner is enough] (https://foofish.net/python-decorator.html)
[Usage and meaning of Python @property] (79221070)
#No.1import loggingdef use_logging(func): def wrapper(): logging.warn("%s is running" % func.__name__) return func() return wrapperdef foo(): print(‘i am foo‘)foo = use_logging(foo)foo()
import loggingdef use_logging(func): def wrapper(): logging.warn("%s is running" % func.__name__) return func() return wrapperuse_loggingdef foo(): print("i am foo")foo()
resut:WARNING:root:foo is runningi am foo
#No.2import loggingdef use_logging(level): def decorator(func): def wrapper(*args, **kwargs): if level == "warn": logging.warn("%s is running" % func.__name__) elif level == "info": logging.info("%s is running" % func.__name__) return func(*args) return wrapper return decorator@use_logging(level="warn")def foo(name=‘foo‘): print("i am %s" % name)foo()
resut:WARNING:root:foo is runningi am foo
#No.3class Rectangle(object): def __init__(self): self.width = 10 self.heigh = 20r = Rectangle()print(r.width, r.heigh)r.width = 1.0print(r.width, r.heigh)
resut:10 201.0 20
#No.4class Rectangle(object): @property def width(self): return self.true_width @property def height(self): return self.true_heights = Rectangle()s.width = 1024s.height = 768print(s.width, s.height)
resut:Traceback (most recent call last): File "D:/fly/Python/test.py", line 23, in <module> s.width = 1024AttributeError: can‘t set attribute
"Adhere to" Selenium+python learning from the beginning of reading code DAY6