[英]Python Error: Why doesn't python recognize my object's type?
class Network():
tables = []
def readInFile():
network = Network()
def printThing(network):
print(len(network.tables))
def main():
network = readInFile()
printThing(network)
if __name__ == "__main__":
main()
gives error: File "thing.py", line 6, in printThing print(len(network.tables)) AttributeError: 'NoneType' object has no attribute 'tables' 给出错误:printThing中的文件“ thing.py”第6行print(len(network.tables))AttributeError:“ NoneType”对象没有属性“ tables”
But the object network isn't NoneType it's clearly of type Network() when it is instantiated in the readInFile function, and type Network() has the attribute tables! 但是对象网络不是NoneType,在readInFile函数中实例化它时,它显然是Network()类型,而Network()类型具有属性表! Please help, thanks
请帮忙,谢谢
You need to return something from your function. 您需要从函数中返回一些东西。 Unless your function has a
return
statement in it, it will return None
除非您的函数中包含
return
语句,否则它将返回None
class Network():
tables = []
def readInFile():
return Network()
def printThing(network):
print(len(network.tables))
def main():
network = readInFile()
printThing(network)
if __name__ == "__main__":
main()
您的函数readInFile()没有return语句,因此始终返回None。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.