where are the "class variables"?

D

Dominik Kaspar

i'm used to java and its strict way of defining variables. so how is
it possible to reach something like a class variable in python?
with the following code i didn't have much succes...

class Server(threading.Thread):
running = 0 (??)

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

def exit(self):
running = 0

thanks
-- dominik
 
B

Bob Gailer

i'm used to java and its strict way of defining variables. so how is
it possible to reach something like a class variable in python?
with the following code i didn't have much succes...

class Server(threading.Thread):
running = 0 (??)

def run(self):
running = 1
while running:
print "do something here..."
def exit(self):
running = 0

prefix running with the class name:
Server.running =1
while Server.running:
...
Server.running = 0

Bob Gailer
(e-mail address removed)
303 442 2625
 
G

Gerrit Holl

Dominik said:
i'm used to java and its strict way of defining variables. so how is
it possible to reach something like a class variable in python?
with the following code i didn't have much succes...

class Server(threading.Thread):
running = 0 (??)

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

def exit(self):
running = 0

This is where self is for: self.running refers to the class
variable 'running'.

Gerrit.
 
J

John Roth

Dominik Kaspar said:
i'm used to java and its strict way of defining variables. so how is
it possible to reach something like a class variable in python?
with the following code i didn't have much succes...

class Server(threading.Thread):
running = 0 (??)

This is correct - it binds 0 to the identifier running in the class.
def run(self):
running = 1

use Server.running
while running:

use while Server.running:
print "do something here..."

def exit(self):
running = 0

use Server.running
thanks
-- dominik

If you want to put an identifier in the class, reference the
class directly, or inherit from object and use a class method.

John Roth
 
T

Tim Rowe

i'm used to java and its strict way of defining variables. so how is
it possible to reach something like a class variable in python?
with the following code i didn't have much succes...

class Server(threading.Thread):
running = 0 (??)

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

def exit(self):
running = 0

thanks
-- dominik

Try:

class Server(threading.Thread):
running = 0
def run(self):
Server.running = 1
while Server.running:
print "Hello!"
self.exit()
def exit(self):
Server.running = 0

If you want class /functions/, though, you need a recent version of
Python.
 
B

Bruno Desthuilliers

Gerrit said:
This is where self is for: self.running refers to the class
variable 'running'.

Gerrit, I'm afraid you answsered too fast. here, 'self.running' will
refer to (and eventually create) an *instance* variable 'running'. If
the OP want a *class* variable, he needs 'Server.running'.

(Now does the OP really want a class variable or did he meant an
instance variable is another question...)

Bruno
 
T

Tom Hanks

Access class members... as members of the class! :)

class Server(threading.Thread):
running = 0

def run(self):
Server.running = 1 # note added "Server."
while Server.running: # ditto, added "Server."
print "do something here..."

def exit(self):
Server.running = 0 # ditto, added "Server."

TTFN,
Tom.
 
G

Gerrit Holl

Gerrit said:
This is where self is for: self.running refers to the class
variable 'running'.

Sorry, this should be:
self.__class__.running

Gerrit.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top