Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > where are the "class variables"?

Reply
Thread Tools

where are the "class variables"?

 
 
Dominik Kaspar
Guest
Posts: n/a
 
      09-30-2003
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"
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      09-30-2003

"Dominik Kaspar" <> wrote in message
news: om...
> 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"



 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      09-30-2003
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

 
Reply With Quote
 
Emile van Sebille
Guest
Posts: n/a
 
      09-30-2003

"Dominik Kaspar" <> wrote in message
news: om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57