Decorators in python : example

Humble Devassy Chirammal Forums Python Programming Decorators in python : example

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1934 Reply
    Humble
    Member
    #!/usr/bin/python
    
    def fun(*args, **kwargs):
    
            def fun1(*args, **kwargs):
                    for a in args:
                            print a+'wrap power'
                    for items in kwargs.items():
                            print items
                    return "True"
    
            def fun2 ( *args, **kwargs):
                    print "I am innocent"
            return fun1
    
    @fun
    def funcham(*args, **kwargs):
            print "I dont have the power or I am wrapped"
            return "nopower"
    
    funcham( "hiii", asas = 12)
    
    
    
    
    Lets execute above code:
    
    [root]$ python decor.py
    hiiiwrap power
    ('asas', 12)
    [root]$
    
    
    so, funcham is decorated here..
    #2027 Reply
    admin
    Guest

    In simple terms “decorator “ returns a function.. The line

    @fun is not exactly the decorator rather its a decorator line. 
    
    Let me know if you need a detailed explanation on this.
Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Decorators in python : example
Your information: