python批量创建对象

1、函数内(locals):

class A():
    def __init__(self):
        self.val = 10
for i in range(10):
    locals()[f'x{i}'] = A()

2、类内:

class A():
    def __init__(self):
        self.val = 10

class B():
    def __init__(self):
        for i in range(10):
            self.__dict__[f'x{i}'] = A()

3、全局(globals):

        和locals类似

你可能感兴趣的:(python)