【堅持】Selenium+Python學習之從讀懂代碼開始 DAY6

來源:互聯網
上載者:User

標籤:test   war   cti   開始   cal   height   print   ast   import   

2018/05/23Python內建的@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的用法及含義](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

【堅持】Selenium+Python學習之從讀懂代碼開始 DAY6

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.