我可以在 Python 2.7 中从文本文件创建对象名称吗?
我正在做一个游戏项目。 我创建了一个对象,星星(对象)。 我想从文本文件动态分配变量的名称。
如果我有一个文本文件:
Sol
Centauri
Vega
我希望程序使用文本文件中的变量名称创建星形(对象)。我希望这个过程自动化,因为我希望创造数百颗星星。 我可以手动写出代码:
Sol = Star(Sol)
Centauri = Star(Centauri)
Vega = Star(Vega)
但是有没有办法自动执行此操作?
本质上,我最终想要的是一个包含星星列表的元组,作为它们自己的对象。然后,当我进行游戏维护时,我可以迭代元组中的所有对象。
I'm working on a game project.
I've created an object, Star(Object).
I want to assign the name of the variables, dynamically, from a text file.
If I have a text file with:
Sol
Centauri
Vega
I want the program to create the Star(Object) with variable names from the text file. I want the process automated, because I'm looking to create hundreds of stars.
I could write the code out by hand:
Sol = Star(Sol)
Centauri = Star(Centauri)
Vega = Star(Vega)
But isn't there a way to automate this?
Essentially, what I eventually want is a tuple with the list of stars, as their own objects. Then, when I am doing game maintenance, I can just iterate over all the objects in the tuple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的问题不清楚。由于您使用的是语法“Star(Centauri)”,这在 Python 中意味着您想要创建一个继承自 Centauri 的名为“Star”的类,这一事实使情况变得模糊。我认为你想要的可能是一个创建不同星星的工厂对象,但是你没有说出星星可能有何不同。据推测,差异在于位置,但您也没有说明如何处理。
根据猜测,最好的选择可能是将星型配置放入 YAML 文件中,并使用 pyYAML 加载它会返回一个可供您使用的 Python 数据结构。
Your question isn't clear. It's clouded by the fact that you're using syntax 'Star(Centauri)', which, in Python, means that you want to create a class called 'Star' that inherits from Centauri. I think what you want is probably a factory object that creates different stars, but then you don't say anything about how the stars might differ. Presumably, the difference is location, but you don't say how that's being handled either.
Best bet, based on guesses, might be to put your star configurations in a YAML file and use pyYAML to load it, which returns a Python data structure ready to go for you.
与“Sol”相同,
但“Sol”可以替换为任何字符串(例如从该文件读取的值)。
另外,您可能需要重新考虑创建这些全局变量 - 它会阻止您在需要时遍历所有星星,并且可能会导致命名冲突。如果您希望这些内容位于字典中,只需将“globals()”替换为您的字典名称即可。
is the same as
except "Sol" can be replaced with any string (eg the values read in from that file).
Also, you may want to rethink making these global variables - it prevents you from being able to iterate through all the stars, should you need to, and could possibly cause naming conflicts. If you want these to be in a dictionary, then just replace "globals()" with the name of your dictionary.
您可能应该为此使用字典。可以创建动态变量名称,但这没有意义,因为要访问,无论如何您都需要间接引用。
You probably should use a dictionary for that. It is possible to create dinamic variable names, but it would make no sense, since to access then you would need an indirect reference anyway.
您可以使用 types 模块在运行时创建类对象:
在上面的示例中,cls 成为您的
class Star
You can use the types module to create class objects at the run time:
In the above example, cls becomes your
class Star
星号的名称不应该是变量的名称。变量名称应反映使用该变量的上下文,例如
destinationStar
或homeStar
。星星的名称应该是
Star
对象的属性,可通过Star.name
访问:通过存储在字典中,您可以搜索特定的
Starstars[name]
来遍历所有星星,或者使用for s in Stars.values()
来迭代所有星星。The name of a star should not be the name of the variable. Variable names should reflect the context in which the variable is used, e.g.
destinationStar
orhomeStar
.A star's name should be a property of the
Star
object, accessed viaStar.name
:By storing in a dictionary, you can search for a particular
Star
withstars[name]
or iterate over all the stars withfor s in stars.values()
, for example.我想动态地分配变量的名称
这很好地表明您的设计完全错误。很难确切地知道你的设计是什么,但我猜你想使用字典。
I want to assign the name of the variables, dynamically
This is a very good indication that your design is completely wrong.It's hard to know exactly what your design is, but I'm going to guess you want to use a dictionary instead.
和一些示例数据:
然后您可以加载您的文件,例如:
结果
为
and some sample data:
then you can load your file like:
and
results in