where are the "class variables"?

John Roth newsgroups at jhrothjr.com
Tue Sep 30 16:38:19 EDT 2003


"Dominik Kaspar" <dokaspar at student.ethz.ch> wrote in message
news:62e9c66e.0309301227.20505db6 at posting.google.com...
> maybe my question was badly formulated. i give it a new try:
> why does the following program give the output "0 0" and not "1 0"?
> why does it loop forever? and how could that problem be fixed?

Because your use of 'running' in the two methods are both
***local*** variables. They are neither instance nor
class variables.

to make them instance variables, they need to be
    self.running.

To make them class variables, they need to be
    Server.running.

Does this help?

John Roth

>
> import threading
>
> class Server(threading.Thread):
>     running = 0
>
>     def run(self):
>         running = 1
>         while running:
>             print "do something here..."
>
>     def stop(self):
>         running = 0
>
> server = Server()
> server.start()
> print server.running        # should print "1"
> server.stop()
> print server.running        # should print "0"






More information about the Python-list mailing list