473,406 Members | 2,439 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,406 software developers and data experts.

global variables

Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex

Jul 18 '05 #1
8 2319
alex wrote:
Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex

Not sensibly, though you can mess around with the __builtin__ namespace
to make values accessible without qualification.

The usual solution is to maintain a config module that establishes
default settings for configuration variables. Other modules that import
config can access (and change) those values using

config.name = value

and so on. Hope this help.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #2
alex wrote:
Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex

Hello,
What about using a class?

Py> class globalVar:
.... pass

Py> globals = globalVar()

Now you can assign 'variables' to it.
And use it anywhere you need it.

Py> globals.image_height = (255,777)
Py> globals.image_mode = 'RGB'
Py> globals.image_names = ['this.jpg', that.jpg']
etc...
hth,
M.E.Farmer

Jul 18 '05 #3
M.E.Farmer wrote:
alex wrote:
is it possible to create 'global' variables that can be seen in all
other classes?


What about using a class?

Py> class globalVar:
... pass

Py> globals = globalVar()


Probably naming it something other than 'globals' would be a good idea
-- otherwise you'll hide the builtin globals() function.

But I agree that the attributes of a class instance (as you suggest) or
the attributes of a module (as Steve Holden suggests) is probably the
right way to go.

Steve
Jul 18 '05 #4
One way to to this is by using keyword args:

class a:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.arg1=arg1
self.arg2=arg2
self.__dict__.update(kwargs)
return

class b:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.__dict__.update(kwargs)
self.a=a(arg1, arg2, **kwargs)
return

class c:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.__dict__.update(kwargs)
self.b=b(arg1, arg2, **kwargs)
return

globals={'global1':1, 'global2':2, 'global3':3, 'global4':4}
C=c(1, 2, **globals)

you will have global1, global2, global3, and global4 attributs
in all classes. If you don't want the attributes, just access
to the values, delete the self.__dict__.update(kwargs) lines.

Larry Bates

alex wrote:
Hi,

is it possible to create 'global' variables that can be seen in all
other classes?

Alex

Jul 18 '05 #5
Steve,
Yes I agree ;) Never use builtin names.
I know better but missed it somehow.
I apologize for any confusion I may have caused.
Thank you Steve for the correction.
M.E.Farmer

Steven Bethard wrote:
M.E.Farmer wrote:
alex wrote:
is it possible to create 'global' variables that can be seen in all other classes?
What about using a class?

Py> class globalVar:
... pass

Py> globals = globalVar()


Probably naming it something other than 'globals' would be a good

idea -- otherwise you'll hide the builtin globals() function.

But I agree that the attributes of a class instance (as you suggest) or the attributes of a module (as Steve Holden suggests) is probably the
right way to go.

Steve


Jul 18 '05 #6
Ok it has been a long day,
In my reply to Steven Bethard , Steve should read Steven ;)

M.E.Farmer

Jul 18 '05 #7
A Steve wrote:
A Steve wrote:
A Steve wrote:


There we go, much clearer ;)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #8
On Fri, 04 Feb 2005 23:56:44 +1000, rumours say that Steve Coghlan
<sc******@iinet.net.au> might have written:
A Steve wrote:
A Steve wrote:
A Steve wrote:


There we go, much clearer ;)


Indeed. I recall some Dan Perl who was advised to change his name to a more
pythonic one, but now I see he misinterpreted the advice.

Am I assimilated or what?
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #9

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.