473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bug in Python class static variable?

I am trying to define a class static variable. But the value of the
static variable seems to be only defined inside the file that the
class is declared. See the code below. When I run "python w.py", I
got:

000=======Hello World
001=======Hello World
002=======Not Initialized
003=======Not Initialized
004=======Not Initialized
005=======Hello World

Looks like even though the class variable "ClassW.w_classStaticVar"
was set inside "startup()", the value never carried over to functions
in "a.py". Is this a bug in Python's static variable handling. I am
running Python 2.4.4.
Ben
#===== file: w.py ========
from a import *

class ClassW:
w_classStaticVar = "Not Initialized"

def initA(self):
print "001=======>", ClassW.w_classStaticVar
obj = ClassA()
obj.init2()
print "005=======>", ClassW.w_classStaticVar

def startup():
ClassW.w_classStaticVar = "Hello World"
wo = ClassW()
print "000=======>", ClassW.w_classStaticVar
wo.initA()

if __name__ == '__main__':
startup()

#===== file: a.py ========
from w import *

class ClassA:
def __init__(self):
print "002=======>", ClassW.w_classStaticVar

def init2(self):
print "003=======>", ClassW.w_classStaticVar
w = ClassW()
print "004=======>", ClassW.w_classStaticVar

Jul 2 '07 #1
2 1985
Bruza <be*****@gmail.comwrote:
I am trying to define a class static variable. But the value of the
static variable seems to be only defined inside the file that the
class is declared. See the code below. When I run "python w.py", I
got:
When you run "python w.py" the *script* w.py is loaded as the module
__main__. Importing a module called 'w' creates a new module which is
unrelated to __main__. If you want access to variables defined in the main
script then you need to import __main__.

Don't use 'from module import *':

The import statements are executed when the interpreter reaches them in the
source. Even if you fix your code to import from __main__, the values you
try to import from __main__ won't exist when the import statement executes:
the first 'from a import *' will load and execute all of module 'a', but
when that executes 'from __main__ import *' it just imports names defined
in the main script *before* a was imported.

In general, don't try to do this: put all your classes into modules and
just put minimal startup code into a script.
Jul 2 '07 #2
On Jul 2, 3:52 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Bruza <benr...@gmail.comwrote:
I am trying to define a class static variable. But the value of the
static variable seems to be only defined inside the file that the
class is declared. See the code below. When I run "python w.py", I
got:

When you run "python w.py" the *script* w.py is loaded as the module
__main__. Importing a module called 'w' creates a new module which is
unrelated to __main__. If you want access to variables defined in the main
script then you need to import __main__.

Don't use 'from module import *':

The import statements are executed when the interpreter reaches them in the
source. Even if you fix your code to import from __main__, the values you
try to import from __main__ won't exist when the import statement executes:
the first 'from a import *' will load and execute all of module 'a', but
when that executes 'from __main__ import *' it just imports names defined
in the main script *before* a was imported.

In general, don't try to do this: put all your classes into modules and
just put minimal startup code into a script.
Duncan,

Thanks for replying. However, I am still confused...
Even if I put "from __main__ import *" in both "a.py" and "w.py", I
still got
the same results. So, how should I re-structure my program to make the
class
static variable works???

Thanks,

Ben

Jul 2 '07 #3

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

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
23
by: Yannick Patois | last post by:
Hi, Under some naming conditions of module files, it seems that python lost class static variables values. It seems only to append when importing a "badly" named module that itself import a...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
4
by: Vedanta Barooah | last post by:
greetings.... in a python nested class is it possible to change the value of the parent class's variable without actually creating an instance of the parent class, consider this code: class...
22
by: Ray | last post by:
Hello, I've been learning Python in my sparetime. I'm a Java/C++ programmer by trade. So I've been reading about Python OO, and I have a few questions that I haven't found the answers for :) ...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
9
by: Daniel Gee | last post by:
A while ago I wrote a class in Java for all kinds of dice rolling methods, as many sides as you want, as many dice as you want, only count values above or below some number in the total, things...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.