Reputation: 790
I am starting with python. I am trying a very simple class-structure, but i get an error.
This is my script:
class controller:
def initLocal(self):
path = input('path:')
local = local()
local.path = path
return local
class location:
pass
class local(location):
path = None
controller = controller()
local = controller.initLocal()
And this is the result i get in the console:
path:a
Traceback (most recent call last):
File "path\to\test.py", line 21, in <module>
local = controller.initLocal();
File "path\to\test.py", line 5, in initLocal
local = local();
UnboundLocalError: local variable 'local' referenced before assignment
I searched for this error, and found it usually has to do something with uncorrect scopes. I however do not see what i am doing wrong here. Is it 'illegal' to have a class instance with the same name as the class?
If i change the initLocal() method to this:
def initLocal(self):
path = input('path:')
locale = local()
locale.path = path
return locale
It works, but i cannot find out why, since controller = controller() does not cause any problems.
Can somebody tell me what i am doing wrong? I have the feeling it might be something really obvious, but i cannot figure out what it is.
Upvotes: 1
Views: 7452
Reputation: 10621
class Location:
pass
class Local(location):
path = None
class Controller:
def initLocal(self):
path = raw_input('path:')
local = Local()
local.path = path
return local
controller = Controller()
local = controller.initLocal()
Upvotes: 1