実現したいこと
デコレーター機能を 完全に、
理解したい。
_
疑問点1、
何故 デコレーターでは、
関数オブジェクトを 戻り値に
しなければ ならないのか?
εの コーディングのように、
内側の 関数を、
実行させたら、
何故 駄目なのか?
_
疑問点2、
α側の コーディングが、
OKで、
β側、γ側の コーディングが、
何故 駄目なのか?
_
疑問点3、
出来れば Δの、
コーデングのように、
インポート構文、変数設定構文、
此等を デコーダー化したいが、
何か 対処法は、
あるのか?
_
皆様、
お手数がけだとは 思いますが、
ご教示を お願いします。
_
前提
環境、
GoogleColaboratory、
又は、
Microsoft配布版Python3.11.1
Visual Studio Cord、
_
バージョン、
Python3.11以上。
_
コードと・相対するエラーメッセージ。
_
α
Python
1y = 0 2def original(z): 3 def wrap(): 4 global y 5 y += 1 6 print(y) 7 z() 8 return wrap 9 10a = b = c = d = original 11 12@a 13@b 14@c 15@d 16def e(): 17 pass 18 19e()
此は Noエラー。
_
β
Python
1y = 0 2def a(z): 3 def wrap(): 4 global y 5 y += 1 6 print(y) 7 z() 8 return wrap 9 10@a 11def b(z): 12 def wrap(): 13 global y 14 y += 1 15 print(y) 16 z() 17 return wrap 18 19@b 20def c(z): 21 def wrap(): 22 global y 23 y += 1 24 print(y) 25 z() 26 return wrap 27 28@c 29def d(z): 30 def wrap(): 31 global y 32 y += 1 33 print(y) 34 z() 35 return wrap 36 37d()
_
β エラー内容、
TypeError Traceback (most recent call last) <ipython-input-9-f9cff7b876d4> in <cell line: 19>() 18 19 @b ---> 20 def c(z): 21 def wrap(): 22 global y TypeError: wrap() takes 0 positional arguments but 1 was given
_
γ
Python
1y = 0 2def a(z): 3 def wrap(*x): 4 global y 5 y += 1 6 print(y) 7 z() 8 return wrap 9 10@a 11def b(z): 12 def wrap(*x): 13 global y 14 y += 1 15 print(y) 16 z() 17 return wrap 18 19@b 20def c(z): 21 def wrap(*x): 22 global y 23 y += 1 24 print(y) 25 z() 26 return wrap 27 28@c 29def d(z): 30 def wrap(*x): 31 global y 32 y += 1 33 print(y) 34 z() 35 return wrap 36 37d()
_
γ エラー内容、
TypeError Traceback (most recent call last) <ipython-input-11-fa5f5226fabe> in <cell line: 19>() 18 19 @b ---> 20 def c(z): 21 def wrap(*x): 22 global y <ipython-input-11-fa5f5226fabe> in wrap(*x) 5 y += 1 6 print(y) ----> 7 z() 8 return wrap 9 TypeError: b() missing 1 required positional argument: 'z'
_
Δ
Python
1def a(z): 2 def wrap(): 3 import numpy as LAC 4 z() 5 return wrap 6 7def b(z): 8 def wrap(): 9 y = LAC.zesos(1) 10 z() 11 return wrap 12 13@a 14@b 15def c(): 16 print(y + 1) 17 18c()
_
Δ エラー内容、
NameError Traceback (most recent call last) <ipython-input-19-d1e441757e14> in <cell line: 18>() 16 print(y + 1) 17 ---> 18 c() 1 frames <ipython-input-19-d1e441757e14> in wrap() 7 def b(z): 8 def wrap(): ----> 9 y = LAC.zeros(1) 10 z() 11 return wrap NameError: name 'LAC' is not defined
_
ε
Python
1def a(z): 2 def wrap(): 3 z() 4 return wrap() 5 6@a 7def b(): 8 pass 9 10b()
_
ε エラー内容、
TypeError Traceback (most recent call last) <ipython-input-22-48d96d592b1b> in <cell line: 10>() 8 pass 9 ---> 10 b() TypeError: 'NoneType' object is not callable
_
試したこと
ネットで、
Pythonデコレーターの 解説を、
読みました。
_
他サイトで、
解説を 求めました。
_
しかし、
此処までの 内容は、
其方には 含まれませんでした。
_
又、
上記のように 色々と、
コーデングを 試しました。
_
補足情報
γは 1だけ、
出力してきました。

あなたの回答
tips
プレビュー