Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can't create object instance!?

    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

    Code:
    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)
    Code:
    if __name__ == '__main__':
    	main()
    Last edited by HeelToeHeroe; 11-15-2010 at 08:36 PM.

  • #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    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.)
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    interesting....

    Quote Originally Posted by Samhain13 View Post
    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.

  • #4
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    my friends found it

    Sorry, I didn't give you the whole code, namely the segment causing the error. I had a for loop with incorrect syntax:
    Code:
    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

  • #5
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    ^ Nice.
    Last edited by Samhain13; 11-17-2010 at 03:29 PM.
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •