Re: Global Variables in OOP and Python
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Global Variables in OOP and Python

From: Brian van den Broek <broek@cc.umanitoba.ca>
Date: Sat Dec 31 2005 - 03:20:52 CET

Gary Herron said unto the world upon 30/12/05 08:03 PM:
> newbie wrote:
>
>
>>Hello,
>>
>>I have questions about global variables in OOP (in general) and Python
>>(in specific). I understand (I think) that global variables are
>>generally not a good idea. However, if there are variables that need to
>>be accessed by a number of classes that exists in separate namespaces
>>(files), what would be the best way to do this?
>>
>>So far, I have approached the problem by making the variables
>>attributes of one class and passing instances of the class as variables
>>to the other class' methods.
>>
>>The other way I thought of is to create a separate class that consists
>>of the variables and to use the
>>
>
>>from <file name> import *
>
>>
>>
>
> That form is deprecated. Better (and certainly clearer to any reader of
> the code) is to leave them in the module.
>
> Define a module, say Parameters, that defines any number of variables
> containing useful values. Then
> import Parameters
> wherever you want and refer to
> Parameters.a
> and
> Parameters.b

<snip>

An other variant seems rarely to come up.

import Parameters as P

provides almost all the brevity of the * variant and none of the dangers.

Best to all,

Brian vdB
Received on Tue Jan 3 03:28:09 2006