How is semaphore variable passed into the following object in python?
Question: After tweaking with this piece of code a few times, I dropped sem.release() in
the Server object WITHOUT actually passing the variable sem into it. But it
works wonderfully... Can't seem to understand why a error wasn't throw for
undeclared variable/reference

    
    
    import threading,time
    
    class Server(threading.Thread):
    
      def __init__(self, hostname):
        super(Server, self).__init__()
        self.__hostname = hostname
    
      def run(self):
        print self.__hostname+' left'
        time.sleep(5)
        print self.__hostname+' back'
        sem.release()
    
    #init
    sem = threading.BoundedSemaphore(2)
    for x in xrange(1,8):
      sem.acquire()
      Server('thread '+str(x)).start()
    


Answer: There's nothing special about accessing a global variable inside a class.

    
    
    x = 3
    
    class xprinter(object):
        def __init__(self):
            print(x)
    
    xprinter() 
    

output:

    
    
    3
    

