0

I have problems with the following code, it says "NameError: global name 'Teater' is not defined" I can not solve it by myself...

teaterLista = []
lista = []
class Teater:


    def __init__(self, teaterNamn, plats, pensionar,vuxen,barn):
        self.teaterNamn = teaterNamn
        self.plats = plats
        self.pensionar = pensionar
        self.vuxen = vuxen
        self.barn = barn

    def readData():
        #x = Teater(x,teaterNamn, plats,pensionar,vuxen,barn)
        dataFile = open('c:/Teater.txt','r')
        for line in dataFile:
            if(line != '\n'):
                  temp = line.split('=',1)[1]
                  lista.append(temp.strip()) #strip tar bort radavslut

        x = Teater(x,lista[0],lista[1],lista[2],lista[3],lista[4])
    #teaterLista[0] = x
    #print(teaterLista[0])

    readData()
4
  • x = Teater(x,lista[0],lista[1],lista[2],lista[3],lista[4]) That line fails, im trying to create a new isntance of Teater with the given parameters. Commented Jul 19, 2015 at 22:33
  • Does calling readData outside of the Teater class work? You might have to do some refactoring to do this -- the readData method may not belong in that class reading multiple Teaters on an instance of one Commented Jul 19, 2015 at 22:36
  • No, the readData does not work outside the class. "name 'readData' is not defined". Do you think it will work if I make a main method and reads data from there? Commented Jul 19, 2015 at 22:45
  • What do you actually intend to accomplish here? Commented Jul 19, 2015 at 23:00

1 Answer 1

1

You call readData() during class definition. In Python a class body is executed during its definition in the contex of the class definition just as normal code would be. As the class is not completely defined at that moment, you cannot create a new instance, yet, thus get the error.

Dedent the whole definition for readData and the following line so all this is executed after the definition of the class has completed. This makes readLine a module-level function, not a class method. This is typical for a factory function.

teaterLista = []
lista = []
class Teater:

    def __init__(self, teaterNamn, plats, pensionar,vuxen,barn):
        self.teaterNamn = teaterNamn
        self.plats = plats
        self.pensionar = pensionar
        self.vuxen = vuxen
        self.barn = barn

def readData():
    #x = Teater(x,teaterNamn, plats,pensionar,vuxen,barn)
    dataFile = open('c:/Teater.txt','r')
    for line in dataFile:
        if(line != '\n'):
              temp = line.split('=',1)[1]
              lista.append(temp.strip()) #strip tar bort radavslut

    x = Teater(lista[0],lista[1],lista[2],lista[3],lista[4])
#teaterLista[0] = x
#print(teaterLista[0])

readData()

Note: x = Teater(x, ... will not work, as x is not defined for the first invocation. If you think about specifying this for the self argument: no need; this is done implicitly. You should read how classes work in the documentation/tutorial.

You have to be careful in Python to correctly indent your code, as that defines the block scope.

Sign up to request clarification or add additional context in comments.

9 Comments

if I Dedent that line it says, "NameError: name 'readData' is not defined"
@user2432626: Yes, I made a mistake in my text. You have to dedent the whole function definition. So that it is not part of the class definition.
@Olaf are you definitely sure about the impossibility of referring to the class within itself? Please see/try the code in my answer.
@Pynchia: Problem is OP's intention is not completely clear. However, Your code calls the method after the class definition is completed. You create a new object, then invoke the method. OP's code does not include self in the argument list (but he does for __init__) of readData, so I assume he does not want it to be a method, but a normal function. As he wrote it, readData would be called diring class definition, then try to create an object of the unfinished class. Well, in Python a lot works, but I do not think that this is his intention.
OK, right. Then it's unclear when he calls readData. I've removed that bit from my answer.
|

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.