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

1
  • 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: stackoverflow.com/q/13482727/748858 Commented May 9, 2014 at 19:56

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.