2014-05-09 49 views
0
class Network(): 
    tables = [] 
def readInFile(): 
    network = Network() 
def printThing(network): 
    print(len(network.tables)) 
def main(): 
    network = readInFile() 
    printThing(network) 
if __name__ == "__main__": 
    main() 

给出错误: 文件 “thing.py”,第6行,在printThing 打印(LEN(network.tables)) AttributeError的: 'NoneType' 对象没有属性 '表'Python错误:为什么Python不能识别我的对象的类型?

但对象网络不是NoneType,它在readInFile函数中实例化时显然是Network()类型,并且类型Network()具有属性表!请帮助,谢谢

+2

虽然与您的初始问题没有关系,但通常不建议将属性作为可变对象(例如Network.tables)作为类属性。您可能需要考虑实例属性。请参阅:http://stackoverflow.com/q/13482727/748858 – mgilson

回答

5

您需要返回东西从你的功能。除非你的函数中有一个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() 
1

你的功能readInFile()没有return语句,因此总是返回无。

相关问题