"Adhere to" Selenium+python learning from the beginning of reading code DAY6

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.