2
我使用globals()来即时创建类的实例。使用全局变量()创建类实例
例如,
animals.py
class Cat():
pass
class Dog():
pass
test.py
#import animals
def create_animal():
# Take 'which_animal' input from
# somewhere in form of string
try:
animal1 = globals()[which_animal]()
catch:
.
.
我这样做是为了避免长的if-else阶梯。
这种方法有什么优点和缺点?
是否有任何替代方法相同?
它是否构成安全威胁?
Python3现在独占?或者只是决定做一个旧式的课程,以获得乐趣? :-)(或者按照我认为的OP代码) – mgilson
@mgilson:在那里,明确了对象的基础。 :-) –
谢谢,我现在对生活感觉更好:-) – mgilson