473,467 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

where are the "class variables"?

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
Jul 18 '05 #1
7 3771
Dominik Kaspar wrote:
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.

--
249. If any one hire an ox, and God strike it that it die, the man who
hired it shall swear by God and be considered guiltless.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

Jul 18 '05 #2

"Dominik Kaspar" <do******@student.ethz.ch> wrote in message
news:62**************************@posting.google.c om...
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

Jul 18 '05 #3
On 30 Sep 2003 11:57:17 -0700, do******@student.ethz.ch (Dominik
Kaspar) wrote:
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.
Jul 18 '05 #4
Gerrit Holl wrote:
Dominik Kaspar wrote:
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, 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

Jul 18 '05 #5
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.
Jul 18 '05 #6
Gerrit Holl wrote:
Dominik Kaspar wrote:
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'.


Sorry, this should be:
self.__class__.running

Gerrit.

--
238. If a sailor wreck any one's ship, but saves it, he shall pay the
half of its value in money.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

Jul 18 '05 #7
At 12:57 PM 9/30/2003, Dominik Kaspar wrote:
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
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: MLH | last post by:
I need a fundamental explanation of Class Modules - something suitable for newbies. Access 2.0 didn't seem to focus on them very much. Now that I'm using Access 97, it seems they're everywhere. thx...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
5
by: Diffident | last post by:
Hello All, I have written a webform which is by default derived from "Page" class. I have coded another utility class with few methods and an object of this class is instantiated from the webfom...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
5
by: Rob | last post by:
In many articles related to VB.net the word "class" is used... How many meanings are there to this word ? "possible to derived a class from another" "forms are full-fledged classes" "base...
10
by: JD | last post by:
Hi all Is it OK to have empty class attribute values, such as <href="url" class="">link</a> ?
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
4
by: Christian Joergensen | last post by:
Hello I stumpled upon this "feature" during my work tonight, and found it a bit confusing: .... class C: .... foobar = 42 .... .... <class __main__.C at 0xb7cf735c>
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.