Python 从导入的模块中调用 class 中的函数
Python Calling a function within a class, within a function, from an imported module
我有这个问题。我现在有2个文件。我正在尝试打印单词 "Hello World Trudy"。我无法绕过它。它一直告诉我我有一个属性错误。我应该怎么做才能解决它?
Traceback (most recent call last):
File "C:\Users\Trudy\Desktop\PythonLearning\test2.py", line 7, in <module>
f.sayHello()
AttributeError: 'NoneType' object has no attribute 'C'
test1.py
def main():
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
if __name__ == "__main__":
main()
test2.py
import test1
def function2():
print ("World")
test1.main().C.function6()
function2()
您不需要 test1 文件中的主函数。只需将 Class C 放在那里。
test1.py
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
test2.py
import test1
def function2():
print ("World")
test1.C().function6()
function2()
我有这个问题。我现在有2个文件。我正在尝试打印单词 "Hello World Trudy"。我无法绕过它。它一直告诉我我有一个属性错误。我应该怎么做才能解决它?
Traceback (most recent call last):
File "C:\Users\Trudy\Desktop\PythonLearning\test2.py", line 7, in <module>
f.sayHello()
AttributeError: 'NoneType' object has no attribute 'C'
test1.py
def main():
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
if __name__ == "__main__":
main()
test2.py
import test1
def function2():
print ("World")
test1.main().C.function6()
function2()
您不需要 test1 文件中的主函数。只需将 Class C 放在那里。
test1.py
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
test2.py
import test1
def function2():
print ("World")
test1.C().function6()
function2()