如何使用名称列表来实例化使用 class 的变量?
How could a list of names be used to instantiate variables using a class?
警告:这个问题不是旧问题 "How do I create a bunch of variables in a loop?" 的重复,因为它使用了带有参数的特殊 class。如果代码主动不尊重 PEP8 让你的眼睛流血,不要再继续了,旅行者!
好的,我正在为详细网络中的对象图编写一些代码,我希望代码本身以特定方式显示。出于这个问题的目的,这意味着我不想编写代码来引用字典,该字典包含在循环中生成的键,该循环引用了我的特殊 class 的实例。我想以我知道的非 Pythonic 的最小方式编写代码,但我糟糕的心理构成可以接受。
那么:下面的函数instantiate
的代码应该怎么写呢?这个想法是这个函数实例化一个变量,其名称使用具有一些参数的特殊 class Relay
指定。
import uuid
def generate_Python_variable_names(
number = 10
):
names = []
while len(names) < number:
name = str(uuid.uuid4()).replace("-", "")
if name[0].isalpha():
names.append(name)
return names
number_relays = 100
for name in generate_Python_variable_names(number = number_relays):
instantiate(name, Relay(configuration = 1)
我假设您处于模块级别。而你想要的是在当前模块中创建新变量。
为此,您可以使用 globals()
函数访问模块的变量。
for name in generate_Python_variable_names(number=number_relays):
obj = globals()
obj[name] = Relay(configuration=1)
如果你打印变量,你会得到这样的东西:
{'Relay': <class '__main__.Relay'>,
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': 'path/to/your/module.py',
'__name__': '__main__',
'__package__': None,
'df55cd5884924e88a4a13371b248d755': <__main__.Relay object at 0x105f66fd0>,
'e11d3a1e3afb4ada801e08d57219e9be': <__main__.Relay object at 0x105f66550>,
...
'ef93466af4a34e7b8ec28568cca664f5': <__main__.Relay object at 0x105f66ed0>,
'generate_Python_variable_names': <function generate_Python_variable_names at 0x105f670c8>,
'name': 'ef93466af4a34e7b8ec28568cca664f5',
'number_relays': 100,
'obj': <Recursion on dict with id=4394090856>,
'uuid': <module 'uuid' from 'path/to/uuid.pyc'>}
如您所见,您的模块中有一百个新变量。
警告:这个问题不是旧问题 "How do I create a bunch of variables in a loop?" 的重复,因为它使用了带有参数的特殊 class。如果代码主动不尊重 PEP8 让你的眼睛流血,不要再继续了,旅行者!
好的,我正在为详细网络中的对象图编写一些代码,我希望代码本身以特定方式显示。出于这个问题的目的,这意味着我不想编写代码来引用字典,该字典包含在循环中生成的键,该循环引用了我的特殊 class 的实例。我想以我知道的非 Pythonic 的最小方式编写代码,但我糟糕的心理构成可以接受。
那么:下面的函数instantiate
的代码应该怎么写呢?这个想法是这个函数实例化一个变量,其名称使用具有一些参数的特殊 class Relay
指定。
import uuid
def generate_Python_variable_names(
number = 10
):
names = []
while len(names) < number:
name = str(uuid.uuid4()).replace("-", "")
if name[0].isalpha():
names.append(name)
return names
number_relays = 100
for name in generate_Python_variable_names(number = number_relays):
instantiate(name, Relay(configuration = 1)
我假设您处于模块级别。而你想要的是在当前模块中创建新变量。
为此,您可以使用 globals()
函数访问模块的变量。
for name in generate_Python_variable_names(number=number_relays):
obj = globals()
obj[name] = Relay(configuration=1)
如果你打印变量,你会得到这样的东西:
{'Relay': <class '__main__.Relay'>,
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': 'path/to/your/module.py',
'__name__': '__main__',
'__package__': None,
'df55cd5884924e88a4a13371b248d755': <__main__.Relay object at 0x105f66fd0>,
'e11d3a1e3afb4ada801e08d57219e9be': <__main__.Relay object at 0x105f66550>,
...
'ef93466af4a34e7b8ec28568cca664f5': <__main__.Relay object at 0x105f66ed0>,
'generate_Python_variable_names': <function generate_Python_variable_names at 0x105f670c8>,
'name': 'ef93466af4a34e7b8ec28568cca664f5',
'number_relays': 100,
'obj': <Recursion on dict with id=4394090856>,
'uuid': <module 'uuid' from 'path/to/uuid.pyc'>}
如您所见,您的模块中有一百个新变量。