“Klasa dekoratora Pythona” Kod odpowiedzi

dekorator klasy Python

# I dont know whether the term "class decorator" is referring to
# using decorators on a class or using a class as a decorator
# so heres both!

## Using a class as a decorator ##
class TestDecorator:

    # This is called when used, func is the function or class provided:
    def __init__(self, func): 
        def wrapper(message):
            print("Wrapper Calling: ", func(message))

        return wrapper

@TestDecorator
def abc(message):
    return message.upper()


## Using a decorator on a class ##
def TestDecorator(provided_class):
    print("Given class: ", provided_class)

    return provided_class

@TestDecorator
class abc: ... 
Orion

Użyj dekoratora w klasie Python

class Test(object):
    def _decorator(foo):
        def magic( self ) :
            print "start magic"
            foo( self )
            print "end magic"
        return magic

    @_decorator
    def bar( self ) :
        print "normal call"

test = Test()

test.bar()
Fantastic Ferret

Metoda klasy dekoratora Pythona

# Here is an example from https://wiki.python.org/moin/PythonDecorators#Examples

@classmethod
def foo (arg1, arg2):
   ....
   
# https://wiki.python.org/moin/PythonDecoratorLibrary has more examples 
CompSciGeek

Klasa dekoratora Pythona

# Python program showing
# use of __call__() method
 
class MyDecorator:
    def __init__(self, function):
        self.function = function
     
    def __call__(self):
 
        # We can add some code
        # before function call
 
        self.function()
 
        # We can also add some code
        # after function call.
 
 
# adding class decorator to the function
@MyDecorator
def function():
    print("GeeksforGeeks")
 
function()
Delta Sierra

Odpowiedzi podobne do “Klasa dekoratora Pythona”

Pytania podobne do “Klasa dekoratora Pythona”

Więcej pokrewnych odpowiedzi na “Klasa dekoratora Pythona” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu