Defining a Python Method Using Decorators

Can someone explain the last line of the following code in detail:

def myMethod(self):
    # do something

myMethod = transformMethod(myMethod)

Why do you want to pass the method definition to another method? And how will it work? Thanks in advance!

+5
source share
5 answers

This is an example of a function wrapper, which is when you have a function that takes a function as an argument and returns a new function that changes the behavior of the original function.

Here is an example of how this can be used, this is a simple wrapper that simply prints 'Enter' and 'Exit' for each call:

def wrapper(func):
    def wrapped():
        print 'Enter'
        result = func()
        print 'Exit'
        return result
    return wrapped

And here is an example of how you could use this:

>>> def say_hello():
...     print 'Hello'
... 
>>> say_hello()  # behavior before wrapping
Hello
>>> say_hello = wrapper(say_hello)
>>> say_hello()  # behavior after wrapping
Enter
Hello
Exit

Python , , , :

>>> @wrapper
... def say_hello():
...     print 'Hello'
... 
>>> say_hello()
Enter
Hello
Exit
+2

?

.

?

, Python.

def decorator(f):
  def wrapper(*args, **kwargs):
    print 'Before!'
    res = f(*args, **kwargs)
    print 'After!'
    return res
  return wrapper

def somemethod():
  print 'During...'

somemethod = decorator(somemethod)

somemethod()
+2

, , - , /, .

, ,

@transformMethod
def myMethod(self):
    # do something

, , @staticmethod, @classmethod, @functools.wraps(), @contextlib.contextmanager .. .. ..

Python ( , 2.6), .

, . , , dict, - .

apply = lambda tobeapplied: lambda f: tobeapplied(f())

@apply(dict)
def mydict():
    yield 'key1', 'value1'
    yield 'key2', 'value2'
print mydict

@apply(set)
def myset():
    yield 1
    yield 2
    yield 1
    yield 4
    yield 2
    yield 7
print myset

?

, ", " , , .

"" , , .

f() -, dict() set().

+1

, Python . - . , : , , ..

, , - "" . , , , .

def print_wrapper(fn):
    def new_wrapped_fn(*args):
        x = fn(*args)
        print("return value of %s is: %s" % (fn.__name__, repr(x)))
        return x
    return new_wrapped_fn

def test(a, b):
    return a * b

test = print_wrapper(test)

test(2, 3)  # prints:  return value of test is: 6

, Python . Google " Python".

0

: " ?" : " ?" , , , , . , , , :

def add_x_to_sequence(x, seq):
    result = []
    for i in seq:
        result.append(i + x)
    return result

def subtract_x_from_sequence(x, seq):
    result = []
    for i in seq:
        result.append(i - x)
    return result

- , , , , , , , for . , , . , ! . , , , , .

, , , :

def my_map(func, x, seq):
    result = []
    for i in seq:
        result.append(func(i, x))
    return result

, , , my_map ( map).

def sub(a, b):
    return a - b

def add(a, b):
    return a + b

:

added = my_map(sub, x, seq)

. , , ; , x , . , , - , . ...

def add_x_to_sequence(x, seq):
    return my_map(add, x, seq)

! , .

. , , . :

def vectorize(func):
    def wrapper(x, seq):
        result = []
        for i in seq:
            result.append(func(i, x))
        return result
    return wrapper

, , , :

def add_x_to_sequence(a, b):
    return a + b
add_x_to_sequence = vectorize(add_x_to_sequence)

, :

@vectorize
def add_x_to_sequence(a, b):
    return a + b

vectorize d, for . ; ; .

0

All Articles