[英]How to convert a string into object
我如何从字符串中调用对象。
这是我的代码:
Class main:
a=(input("file name"))
b=(input("insert a word"))
archive.relacao(b,a)
Class archive:
def relacao(self,word,document):
t=doc.contsimple()[word]
方法 contsim 位于类 Document 中
错误是: NameError: global name 'doc' is not defined
如果你这样做,它会起作用吗?
Class main:
a=(input("file name"))
b=(input("insert a word"))
archive.relacao(b,a)
Class archive:
def relacao(self,word,document):
t=document.contsimple()[word]
因为这立即引起了我的注意 - archive.relacao 被传递了文件名,它作为document
。 但document
没有使用- doc
时使用。 doc
未定义,因此名称错误。
那么有几件事可能会导致该错误:
确保在使用之前定义doc
。
Python 是一种解释型语言,所以如果
t=doc.contsimple()[word]
声明是在你定义一些doc
对象之前 Python 会抱怨的。
你的意思是document
而不是文档
函数relacao
正在接收一个document
参数,因此您可能输入错误的doc
var。 改变:
t=doc.contsimple()[word]
到:
t=document.contsimple()[word]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.