where are the "class variables"?

Discussion in 'Python' started by Dominik Kaspar, Sep 30, 2003.

  1. 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?

    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"
     
    Dominik Kaspar, Sep 30, 2003
    #1
    1. Advertising

  2. Dominik Kaspar

    John Roth Guest

    "Dominik Kaspar" <> wrote in message
    news:...
    > 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"
     
    John Roth, Sep 30, 2003
    #2
    1. Advertising

  3. Dominik Kaspar

    Jeff Epler Guest

    On Tue, Sep 30, 2003 at 01:27:15PM -0700, Dominik Kaspar wrote:
    > 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 inside functions, an assignment to a simple name, like
    running = 1
    refers to a local variable, unless there is a "global" statement in
    which case it refers to a module-level variable:
    global running
    running = 1
    (this is still not what you want)
    You want to refer to Server.running, as suggested in another message:
    def run(self):
    Server.running = 1

    jeff
    PS The first server.running might print 0 if the first line of
    Server.run hasn't executed the first statement yet, of course, but
    that's another story
     
    Jeff Epler, Sep 30, 2003
    #3
  4. "Dominik Kaspar" <> wrote in message
    news:...
    > 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?


    As written, you have a local varaible running in method run that gets set
    and never changes. The class variable you're looking for is Server.running,
    like this:

    import time, threading

    class Server(threading.Thread):
    running = 0

    def run(self):
    Server.running = 1
    while Server.running:
    print "do something here..."
    time.sleep(5)

    def stop(self):
    Server.running = 0

    server = Server()
    server.start()
    print server.running # should print "1"
    server.stop()


    --
    Emile van Sebille
     
    Emile van Sebille, Sep 30, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    35,954
    jporter892
    Jun 6, 2011
  2. Replies:
    1
    Views:
    247
  3. MarkB
    Replies:
    4
    Views:
    324
    Sarkar
    Aug 7, 2009
  4. Eric D.
    Replies:
    3
    Views:
    186
    Jeremy Henty
    Feb 1, 2006
  5. Ralph Shnelvar
    Replies:
    29
    Views:
    797
    David Masover
    Nov 30, 2009
Loading...

Share This Page