在尝试使用类和方法以及如何在它们之间传递变量时,我编写了两个脚本来尝试理解这些机制。 在这样做的过程中,我遇到了一个问题:我的一个函数没有定义:
NameError: name 'exclaim' is not defined
我以为使用自我可能会解决问题,但我只是绕了一圈
NameError: name 'self' is not defined
关于这一点,我遇到了几个来源,这使我了解了方法的缩进级别,并通过HelloWorld.惊呼() 这是同一个问题。你知道吗
请看我的代码:(script1)
import datasource
class HelloWorld:
def exclaim():
number1 = input("enter a number")
datasource.Class2.method3.impvariable1 = number1
def main():
HelloWorld.exclaim()
print(datasource.Class1.method1.variable1)
print(datasource.Class2.method2.variable2)
print(datasource.Class2.method3.variable3)
if __name__ == '__main__':
main()
脚本2:
#datasource.py
class Class1:
def method1():
variable1 = "Hello "
class Class2:
def method2():
variable2 = "World"
def method3():
impvariable1 = 0
variable3 = "!"
for x in range(impvariable1):
variable3 = variable3 + "!"
我也尝试过(其他迭代的数量为100)
#datahandler.py
import datasource
class HelloWorld:
def exclaim(self):
number1 = input("enter a number")
datasource.Class2.method3.impvariable1 = number1
def main(self):
HelloWorld.exclaim(self)
print(datasource.Class1.method1.variable1)
print(datasource.Class2.method2.variable2)
print(datasource.Class2.method3.variable3)
if __name__ == '__main__':
main(self)
产生的
NameError: name 'self' is not defined
相关问题 更多 >
编程相关推荐