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()

gives error: File "thing.py", line 6, in printThing print(len(network.tables)) AttributeError: 'NoneType' object has no attribute '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! Please help, thanks

  • 2
    While not related to your initial question, it's usually not adviseable to put an attribute which is a mutable object (e.g. your Network.tables) as a class attribute. You might want to consider an instance attribute instead. see: http://stackoverflow.com/q/13482727/748858 – mgilson May 09 '14 at 19:56

2 Answers2

5

You need to return something from your function. Unless your function has a return statement in it, it will 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()
mgilson
  • 300,191
  • 65
  • 633
  • 696
1

Your function readInFile() does not have a return statement and therefore always returns None.

Ashley G
  • 11
  • 2