[Python] I made a decorator that doesn't seem to have any use.

Introduction

Do you guys make decorators? Embarrassingly, I learned the details ** today **. Even if I noticed the existence, I ignored it until today, but since I studied, I am writing with the intention of outputting something.

What I made

Immediately, I tried to make something like this.

deco_test.py


def decof(Func,*_args,**_kwargs):
    def _deco(func):
        def wrapper(*args,**kwargs):
            return Func(func(*args,**kwargs),*_args,**_kwargs)
        return wrapper
    return _deco

def decob(Func,*_args,**_kwargs):
    def _deco(func):
        def wrapper(*args,**kwargs):
            return Func(*_args,func(*args,**kwargs),**_kwargs)
        return wrapper
    return _deco

Roughly speaking, you can create a function ** that allows the specified function to receive the return value of the function to be wrapped. There are two because it is used properly depending on whether the return value is received as the first argument (decof ()) or the last argument (decob ()). It's hard to understand in words, so when I demonstrate it,

deco_test.py


import numpy as np
from functools import reduce
from operator import sub

@decob(reduce,sub)
@decof(np.array,dtype=int)
@decof(list)
@decof(str)
@decob(pow,2)
@decof(pow,2)
def add(a,b):return a+b

print(add(3,4))

output


-55
#3+4 -> 7**2 -> 2**49 -> '562949953421312' -> ['5', '6', '2', '9', '4', '9', '9', '5', '3', '4', '2', '1', '3', '1', '2']
# -> array([5, 6, 2, 9, 4, 9, 9, 5, 3, 4, 2, 1, 3, 1, 2]) -> 5-6-2-9-4-9-9-5-3-4-2-1-3-1-2 -> -55

You can clearly express (?) A function relay like this. that's all.

Summary

I've avoided it until now, but it seems to be quite convenient depending on how it is used, so if you come up with something again, I'd like to introduce it here. Thank you for reading to the end.

Recommended Posts

[Python] I made a decorator that doesn't seem to have any use.
[Python3] I made a decorator that declares undefined functions and methods.
I want to use a wildcard that I want to shell with Python remove
I made a library that adds docstring to a Python stub file.
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
I made a web application in Python that converts Markdown to HTML
I made a Line Bot that uses Python to retrieve unread Gmail emails!
A story that I was addicted to when I made SFTP communication with python
I made a VM that runs OpenCV for Python
I made a Python module to translate comment outs
I made a python library to do rolling rank
I made a python text
I made a package to filter time series with python
I made a toolsver that spits out OS, Python, modules and tool versions to Markdown
I made a Line-bot using Python!
I made a fortune with Python.
I made a program to collect images in tweets that I liked on twitter with Python
I made a function to crop the image of python openCV, so please use it.
I made a daemon with Python
[Python] I made a Line bot that randomly asks English words.
I made a package that can compare morphological analyzers with Python
[Python] A memo that I tried to get started with asyncio
I made a shuffle that can be reset (reverted) with Python
[python] I made a class that can write a file tree quickly
I made a garbled generator that encodes favorite sentences from UTF-8 to Shift-JIS (cp932) in Python
I made Othello to teach Python3 to children (4)
I made a payroll program in Python!
I made a character counter with Python
I made Othello to teach Python3 to children (2)
I want to use jar from python
I want to build a Python environment
I have a question! (Python, django) Easy
I made Othello to teach Python3 to children (5)
I made a script to display emoji
I made a Hex map with Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a password generator to teach Python3 to children (bonus) * Completely remade
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a Discord bot in Python that translates when it reacts
I tried to develop a Formatter that outputs Python logs in JSON
I tried to discriminate a 6-digit number with a number discrimination application made with python
[Python] I made a utility that can access dict type like a path
I made a script in python to convert .md files to Scrapbox format
How to use hmmlearn, a Python library that realizes hidden Markov models
I made a library konoha that switches the tokenizer to a nice feeling
I made a tool that makes decompression a little easier with CLI (Python3)
A memorandum because I stumbled on trying to use MeCab in Python
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a program to check the size of a file in Python
I made a function to see the movement of a two-dimensional array (Python)
I made a module PyNanaco that can charge nanaco credit with python
[Python] I made a function that can also use regular expressions that replace character strings all at once.
I made a python dictionary file for Neocomplete
python I don't know how to get the printer name that I usually use.
I made a competitive programming glossary with Python