坚持Selenium+Python学习之从读懂代码开始 DAY6
Posted flyin9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了坚持Selenium+Python学习之从读懂代码开始 DAY6相关的知识,希望对你有一定的参考价值。
2018/05/23
Python内置的@property装饰器
[@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之禅 :理解 Python 装饰器看这一篇就够了](https://foofish.net/python-decorator.html)
[python @property的用法及含义](https://blog.csdn.net/qq_41673534/article/details/79221070)
#No.1
import logging
def use_logging(func):
def wrapper():
logging.warn("%s is running" % func.__name__)
return func()
return wrapper
def foo():
print(‘i am foo‘)
foo = use_logging(foo)
foo()
import logging
def use_logging(func):
def wrapper():
logging.warn("%s is running" % func.__name__)
return func()
return wrapper
use_logging
def foo():
print("i am foo")
foo()
resut:
WARNING:root:foo is running
i am foo
#No.2
import logging
def 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 running
i am foo
#No.3
class Rectangle(object):
def __init__(self):
self.width = 10
self.heigh = 20
r = Rectangle()
print(r.width, r.heigh)
r.width = 1.0
print(r.width, r.heigh)
resut:
10 20
1.0 20
#No.4
class Rectangle(object):
@property
def width(self):
return self.true_width
@property
def height(self):
return self.true_height
s = Rectangle()
s.width = 1024
s.height = 768
print(s.width, s.height)
resut:
Traceback (most recent call last):
File "D:/fly/Python/test.py", line 23, in <module>
s.width = 1024
AttributeError: can‘t set attribute
以上是关于坚持Selenium+Python学习之从读懂代码开始 DAY6的主要内容,如果未能解决你的问题,请参考以下文章
坚持Selenium+Python学习之从读懂代码开始 DAY5