码迷,mamicode.com
首页 > 编程语言 > 详细

【坚持】Selenium+Python学习之从读懂代码开始 DAY6

时间:2018-05-23 17:03:49      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:test   war   cti   开始   cal   height   print   ast   import   

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

标签:test   war   cti   开始   cal   height   print   ast   import   

原文地址:https://www.cnblogs.com/flyin9/p/9075410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!