1

The code I have is:

class New(Server):
    noOfCl = 0        

    def onConnect(self, socket):
        print "Client connected"
        print (noOfCl+=1)

I am receiving the following error: UnboundLocalError: local variable 'noOfCl' referenced before assignment. From what I understand, I'm declaring noOfCl before I am referencing it. Anyone have any ideas as to what I'm doing wrong? Thanks

1
  • 1
    This code does not throw the error you describe. It throws a plain NameError. Commented Apr 29, 2012 at 19:57

1 Answer 1

6

As noOfCl is a Class Variable you need to prefix the Class Name before it.

class New(Server):
    noOfCl = 0        

    def onConnect(self, socket):
        print "Client connected"
        New.noOfCl+=1
        print(New.noOfCl)

Also your in-place update when calling the print function/statement is not supported in Python.

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

2 Comments

The parentheses after print are redundant
@San4ez: Depends whether its 3.x or 2.x. This will work irrespective of the Python's Version.

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.