Note: Python Decorator

Just a function

def hoge():
  print "aaaaaaabb"

hoge()
aaaaaaabb

There is nothing strange about it.

Decorate the function

In fact, Python is @ (function name) You can create a nested structure by adding the description before the function definition. So if you play with it a little


def deco(func):
  def aaa():
    print "wei soiya"
    func()
  return aaa

@deco
def hoge():
  print "aaaaaaabb"

hoge()
wei soiya
aaaaaaabb

A sentence will be added before

In the above example

@deco
def hoge

By

hoge()

But

deco(hoge)()

Is equivalent to.

Have multiple decorators + arguments

It seems that you can combine multiple decorators, or you can pass arguments to the decorator separately by biting the wrapper.

def deco1(name):
    def wrapper(func):
        def aaaa():
            print "your name is "+name
            print "deco1 called!"
            return func("aaa","www",yakiniku="teishoku")
        return aaaa
    return wrapper

def deco2(func):
    def bbbb(*args, **kwargs):
        print "wrap2 called!"
        return func(*args, **kwargs)
    return bbbb

@deco1('nanntara-kanntara-')
@deco2
def eee(*args, **kwargs):
    print args
    print kwargs
    return "done"

print eee()
your name is nanntara-kanntara-
deco1 called!
wrap2 called!
('aaa', 'www')
{'yakiniku': 'teishoku'}
done

I referred to the following site.

http://hangar.runway7.net/python/decorators-and-wrappers

Recommended Posts

Note: Python Decorator
Note: Python
Python note
Python study note_002
Python programming note
[Python] Learning Note 1
Python study note_004
Python study note_003
[Note] openCV + python
Python function decorator
Python beginner's note
[Note] future sentence ~ Python ~
[Note] File reading ~ Python ~
python decorator to retry
python decorator usage notes
Note to daemonize python
Note: python Skeleton Nya
Python basic grammar note (4)
Python basic grammar note (3)
Python Tkinter Primer Note
Python decorator operation memo
I tried Python> decorator
Note
Decorator 1
Python
Python Input Note in AtCoder
[Note] Operate MongoDB with Python
Note
Note
Decorator 2
[WIP] Fluent Python Study Note
Python higher-order function (decorator) sample
3 months note for starting Python
[AtCoder] ABC165C Personal Note [Python]
Note that it supports Python 3
Decorator
A note about [python] __debug__
Decorator
Python Note: About comparison using is
[Note] Project Euler in Python (Problem 1-22)
[Introduction to Udemy Python 3 + Application] 57. Decorator
Python: A Note About Classes 1 "Abstract"
[Python] What is @? (About the decorator)
(Note) Be careful with python argparse
[Note] Hello world output with python
boto3 (AWS SDK for Python) Note
Foreign Key in Python SQLite [Note]
Create a Python general-purpose decorator framework
Python Note: Get the current month
A note about mock (Python mock library)
kafka python
Django note 4
Python basics ⑤
A note where a Python beginner got stuck
python + lottery 6
Create a Python function decorator with Class
Python Summary
Built-in python
python note: when easy_install is not available
Python comprehension
Python technique