Python装饰器@property

代码示例

22
0

python装饰器

# decorators are user to decorate functions like what to do before/after calling the function
import time

def delay_decorator(function):
    def wrapper_function():
        print("-----------------------I am gonna greet you--------------------------")
        time.sleep(2)# waits for 2 seconds
        function()   # calls the function
        time.sleep(1)# waits for 1 sec
        print("------------------How do you feel about that greet?-------------------")
    return wrapper_function

@delay_decorator
def greet():
    print("Helllllloooooooooo")

greet()
7
0

python装饰器

#Decorator are just function that take function as first
#parameter and return a function
def logging(f):
  def decorator_function(*args, **kwargs):
    print('executing '+f.__name__)
    return f(*args, **kwargs)
  return decorator_function
#Use it like this
@logging
def hello_world():
  print('Hello World')
#calling hello_world() prints out:
#executing hello_world
#Hello World
2
0

python中的@property

class Person:
    def __init__(self, name):
        self._name = name

    def get_name(self):
        print('Getting name')
        return self._name

    def set_name(self, value):
        print('Setting name to ' + value)
        self._name = value

    def del_name(self):
        print('Deleting name')
        del self._name

    # Set property to use get_name, set_name
    # and del_name methods
    name = property(get_name, set_name, del_name, 'Name property')

p = Person('Adam')
print(p.name)
p.name = 'John'
del p.name
0
0

python装饰器

import functools

# A decorator is a Higher order function "_with_logging"
# It takes the function to be decorated as its argument
# In order to pass in some arbitrary arguments, it must be wrapped into
# another HOF (Higher order function) that will receive the inputs
def with_logging(level=logging.DEBUG, msg = None):
    def _with_logging(fn):
        # 'wraps' is a HOF that will give fn's name and docs to decorated_fn i.e. 
        #   decorated_fn.__name__ = fn.__name__
        #   help(decorated_fn) = help(fn)
        @functools.wraps(fn)
        def decorated_fn(*args, **kwargs):
            res = fn(*args, **kwargs)
            print("\n***************", f"\n{msg}", "\nExecuting with Args: ", *args, **kwargs)
            logging.log(level, res)
            return res
        return decorated_fn
    return _with_logging
  
# Used this way
@with_logging(level=logging.DEBUG, msg="Some awesome comment")
def hello_world(name):
  return f'Hello World {name}'
  
# Results after calling hello_world("John")
#
# ***************
# Some awesome comment
# Executing with Args: John
# Hello World John
-1
0

python中的@property

Getting name
The name is: Adam
Setting name to John
Deleting name

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................