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)
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
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