import sys # 自定义的函数 def hello(d): print(f"hello world {d}") # 调用函数 hello 方法一 getattr(sys.modules[__name__], 'hello')('first demo') # 获取当前脚本名称 print(sys.modules[__name__]) # 在类中调用 locals 函数 class New(): @staticmethod def run_fnc_body(fc_name,b_data): # rdb.set_trace() return getattr(sys.modules[__name__], fc_name)(b_data) def __init__(self): self.run_fnc_body('hello','d') self.run_fnc_body('hello','f') @staticmethod def run_fnc_body(fc_name, b_data): return getattr(sys.modules[__name__], fc_name)(b_data) a = New() a.run_fnc_body('hello','f')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import
sys
# 自定义的函数
def
hello
(
d
)
:
print
(
f
"hello world {d}"
)
# 调用函数 hello 方法一
getattr
(
sys
.
modules
[
__name__
]
,
'hello'
)
(
'first demo'
)
# 获取当前脚本名称
print
(
sys
.
modules
[
__name__
]
)
# 在类中调用 locals 函数
class
New
(
)
:
@
staticmethod
def
run_fnc_body
(
fc_name
,
b_data
)
:
# rdb.set_trace()
return
getattr
(
sys
.
modules
[
__name__
]
,
fc_name
)
(
b_data
)
def
__init__
(
self
)
:
self
.
run_fnc_body
(
'hello'
,
'd'
)
self
.
run_fnc_body
(
'hello'
,
'f'
)
@
staticmethod
def
run_fnc_body
(
fc_name
,
b_data
)
:
return
getattr
(
sys
.
modules
[
__name__
]
,
fc_name
)
(
b_data
)
a
=
New
(
)
a
.
run_fnc_body
(
'hello'
,
'f'
)
|