如何将字符串转换为obj

2024-10-01 00:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从字符串调用对象。在

这是我的代码:

 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]

contsimple方法在类文档中

错误是: 名称错误:未定义全局名称“doc”


Tags: 对象字符串代码name名称inputdocmain
2条回答

好吧,有几件事会导致这个错误:

确保在使用前定义doc

Python是一种解释语言,因此如果

t=doc.contsimple()[word]

语句是在您定义一些doc对象之前,Python会对此抱怨。在

你是说document而不是doc

函数relacao正在接收一个document参数,因此可能您键入了doc变量。 更改:

t=doc.contsimple()[word]

收件人:

t=document.contsimple()[word]

如果你这样做有效吗?在

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]

因为这一点在我看来立竿见影-存档.relacao传递文件名,它采用document。但是document没有使用-doc被使用。doc未定义,因此出现名称错误。在

相关问题 更多 >