[Python3] I made a decorator that declares undefined functions and methods.

1. Preparation

Create the following module (= py file) and do import.

undefined.py


class undefined error(Exception):
    def __init__(self, f): super().__init__(str(f)+"Is undefined.")
class undefined:
    def __init__(self, f):raise undefined error(f)
class undefined method:
    def __init__(self, m): self.m = m
    def __call__(self, *_, **__): raise Error(self.m)

2. How to use

2-1. In-function function (if not an object-oriented method)

First, put the decorator @ undefined on top of the undefined function def as follows.

python


from undefined import*

def f_out():
    @undefined
    def f_in():pass
    return f_in

And when we evaluate f_out (), we get:

Undefined Error: f_in is undefined


Traceback (most recent call last):
  File "<pyshell#301>", line 1, in <module>
    f_out()
  File "~~~~.py", line 104, in f_out
    def f_in():pass
  File "~~~~.py", line 100, in __init__
raise undefined error(f)
Undefined Error: <function f_out.<locals>.f_in at 0x0000023470EFADC0>Is undefined

2-2. For object-oriented methods

First, add the decorator @ undefined method on top of the undefined method def as follows.

python



class Test:
    def __init__(self):pass

    @Undefined method
    def mtd(self):pass

And when we evaluate Test (). Mtd (), we get:

Undefined Error: Test.mtd is undefined


Traceback (most recent call last):
  File "<pyshell#309>", line 1, in <module>
    Test().mtd()
  File "~~~~.py", line 101, in __call__
    def __call__(self, *args, **kwargs):raise undefined error(self.m)
Undefined Error: <function Test.mtd at 0x0000021AD5D3AF70>Is undefined

Note: When you create a method in a class, the processing system executes the method once without explicitly calling the class or its object. Method (*). Therefore, if you simply replace @ undefined method with @ undefined, when an undefined method exists, even if the class is never called or an object is not created, it is undefined. It will now return Error.

Recommended Posts

[Python3] I made a decorator that declares undefined functions and methods.
[Python] I made a decorator that doesn't seem to have any use.
I made a VM that runs OpenCV for Python
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
I made a LINE BOT with Python and Heroku
I made a python text
I made a Chatbot using LINE Messaging API and Python
I made a Line-bot using Python!
I made a fortune with Python.
I made a daemon with Python
I made a toolsver that spits out OS, Python, modules and tool versions to Markdown
[Python] I made a Line bot that randomly asks English words.
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a package that can compare morphological analyzers with Python
I made a Nyanko tweet form with Python, Flask and Heroku
I made a shuffle that can be reset (reverted) with Python
I made a library that adds docstring to a Python stub file.
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
[python] I made a class that can write a file tree quickly
I made a payroll program in Python!
I made a character counter with Python
I made a Hex map with Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a web application in Python that converts Markdown to HTML
I made a Discord bot in Python that translates when it reacts
[Python] I made a utility that can access dict type like a path
I made a tool that makes decompression a little easier with CLI (Python3)
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a module PyNanaco that can charge nanaco credit with python
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
A Java programmer studied Python. (About functions (methods))
A memo that I wrote a quicksort in Python
I made a GUI application with Python + PyQt5
A nice nimporter that connects nim and python
I made a Twitter fujoshi blocker with Python ①
I wrote a class in Python3 and Java
[Python] I made a Youtube Downloader with Tkinter.
A story about modifying Python and adding functions
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
I made a Line Bot that uses Python to retrieve unread Gmail emails!
I made a Docker image that can call FBX SDK Python from Node.js
I made a familiar function that can be used in statistics with Python
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Python] A program that finds the minimum and maximum values without using methods
In Python, I made a LINE Bot that sends pollen information from location information.
A story that I was addicted to when I made SFTP communication with python
[Python] I made a web scraping code that automatically acquires the news title and URL of Nikkei Inc.
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
I made a Twitter BOT with GAE (python) (with a reference)