...

View Full Version : can't create object instance!?



HeelToeHeroe
11-15-2010, 05:47 PM
Hello all,
first post here. I am a programming student learning python (I know some other languages pretty well) and I am baffled by this problem. I normally code in linux, but this program is for windows(don't see anything that makes this matter, but just in case).
I am trying to make an instance of the class Employee. When I try to do so, it says "Unbound Local Error: Local variable 'Employee' referenced before assignment". The thing is, I'm trying to create it, lol. I believe I have the right number of arguments for the __init__ function (self doesn't count when you create an instance, right?) so it shouldn't be looking for a different Employee class. Thanks for the help.
-HeelToe


import time
import os

global emps

def main():
startup()
loopit()
return

class Employee:
def __init__(self, ID, namef, namel):
self.PID =ID
self.fname = namef
self.lname = namel
self.timein = []
self.timeout = []
self.clockedin = 0

def show_wk_times():
print "showing week times for ", self.PID

def show_all_times():
print "showing all times for ", self.PID

def startup():
#os.system("cls")
global emps
emps = []
print "starting up!"

def loopit():
running = 1
global emps
#x = Employee(6912, 'Eric', 'Olsen')
emps.append(Employee(6912, "Eric", "Olsen"))
(code continues)


if __name__ == '__main__':
main()

Samhain13
11-16-2010, 02:52 AM
self doesn't count when you create an instance, right?

Right.

Weird though. I just ran your script with the x = Employee line uncommented and the thing works. (Using Python 2.6 in Ubuntu.)

HeelToeHeroe
11-16-2010, 03:46 PM
Right.

Weird though. I just ran your script with the x = Employee line uncommented and the thing works. (Using Python 2.6 in Ubuntu.)

I just tried running python 2.6 in Ubuntu on my laptop and got the same error...I'll have to scrutinize this later. Thanks for the help though.

HeelToeHeroe
11-16-2010, 08:30 PM
Sorry, I didn't give you the whole code, namely the segment causing the error. I had a for loop with incorrect syntax:

for Employee in emps:
....

I thought the 'Employee' had to be the name of the object contained in the list, but it knows what's in the list so I changed it to 'E' and it works. Even though the for loop was after the constructor, it saw 'Employee' as an unbound variable. Thanks though.
-Heeltoe

Samhain13
11-17-2010, 02:26 PM
^ Nice. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum