Python从导入的模块调用类内、函数内的函数
我有这个问题。我现在有两个文件。我正在试着打印“Hello World Trudy”这几个字。我不能到处做这件事。它一直告诉我我有一个属性错误。我该怎么做才能修好它Python从导入的模块调用类内、函数内的函数,python,algorithm,python-2.7,python-3.x,python-import,Python,Algorithm,Python 2.7,Python 3.x,Python Import,我有这个问题。我现在有两个文件。我正在试着打印“Hello World Trudy”这几个字。我不能到处做这件事。它一直告诉我我有一个属性错误。我该怎么做才能修好它 Traceback (most recent call last): File "C:\Users\Trudy\Desktop\PythonLearning\test2.py", line 7, in <module> f.sayHello() AttributeError: 'NoneType' objec
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'
test2.py
import test1
def function2():
print ("World")
test1.main().C.function6()
function2()
test1文件中不需要main函数。就让C班的学生在那里 test1.py
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
import test1
def function2():
print ("World")
test1.C().function6()
function2()
test2.py
class C:
def function6(self):
print ("Hello")
def function7(self):
print ("Trudy")
def sayHello():
C().function6()
def sayWorld():
C().function7()
import test1
def function2():
print ("World")
test1.C().function6()
function2()
main
函数是怎么回事?它为什么存在?另外,您只使用了一个Python版本。不要标记多个。main()
是一个不返回任何内容的函数(因此其返回类型为NoneType
)。不能从函数块外部访问函数块内部定义的类。为什么要在函数内部声明类?为什么要执行C().function6()
而不是在某处存储C
的实例?你到底想干什么?你想用这段代码解决什么问题?一些推荐的阅读材料,正如文档所建议的,请先阅读关于作用域和名称空间的内容。嘿,伙计们,很抱歉。我对Python真的很陌生!将阅读文档并试图找出答案。谢谢大家!