简体   繁体   English

Python错误:为什么python无法识别我对象的类型?

[英]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.

相关问题 为什么python 2.7.10不能识别我的.pystartup和.inputrc? - Why doesn't python 2.7.10 recognize my .pystartup and .inputrc? 在 python 中作为字节对象传递时,为什么密码不能在 s3 中打开我的 zip 文件? - Why doesn't the password open my zip file in s3 when passed as a bytes object in python? Python包导入错误 - Python无法识别包 - Python Package Import Error--Python Doesn't Recognize Package 为什么HTML发送HTML参数时python Dajaxice函数无法识别? - Why doesn't python Dajaxice function recognize when HTML sends argument(s)? 为什么我的Python脚本无法识别导入模块中的类? - Why doesn't my Python script recognize a class from an imported module? 为什么 Python 不能识别我的 utf-8 编码源文件? - Why doesn't Python recognize my utf-8 encoded source file? 为什么我的python子类无法识别超类的属性? - Why my python subclass doesn't recognize attribute from super class? 为什么Visual Studio Code不能识别我的WSL bash python? - Why doesn't Visual Studio Code recognize my WSL bash python? graphene-python 无法识别“content_type”字段 - graphene-python doesn't recognize ´content_type´ field Python不会将-1识别为False? - Python doesn't recognize -1 as False?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM