Re: Global Variables in OOP and Python



On Fri, 30 Dec 2005 15:03:54 -0800, 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.


Do you mean something like this?

# Module care_and_feeding

import birds
import foods

def feed_my_pet():
pet = birds.Parrot("Norwegian Blue")
snack = foods.Spam("A tasty meat-like treat")
pet.eats(snack)
return "Yummy!"


That is a good way of handling the problem.



> 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 *
>
> in all of the files (namespaces) where it is needed.

That's a bad way of handling it. It has all the worst aspects of using
global variables, plus the worst aspects of import * (namespace pollution
and shadowing of existing names).

Unless I'm badly mistaken, Guido has decided that "from module import *"
was a mistake, and that will be removed from the (legendary) Python 3, if
and when it gets created. In the meantime, it is highly recommended that
you don't use that form.


> Is there a better way?
>
> Are the two ideas presented above acceptable? If so, is one better than
> the other from an OOP POV?


I think the first way is fine, but of course the Devil is in the details:
I can't judge your code without seeing it.


--
Steven.

.



Relevant Pages

  • Global Variables in OOP and Python
    ... I have questions about global variables in OOP and Python ... be accessed by a number of classes that exists in separate namespaces ...
    (comp.lang.python)
  • Re: Global Variables in OOP and Python
    ... I have questions about global variables in OOP and Python ... be accessed by a number of classes that exists in separate namespaces ...
    (comp.lang.python)
  • Re: Global Variables in OOP and Python
    ... I have questions about global variables in OOP and Python ... be accessed by a number of classes that exists in separate namespaces ... You can even add runtime parameters to the module during your initialization code, and those will be available wherever Parameters is imported: ...
    (comp.lang.python)
  • Re: How to pass a global variable to a module?
    ... I just start to use python and love this language. ... problem when I try to save my functions in a separate file. ... In Python, as in many other languages, I'd advise that you think about whether your variable needs to be global, or whether you could simply pass the variable to the function as a parameter. ... I can understand the point that global variables tends to mess up ...
    (comp.lang.python)
  • Problem with global variables
    ... I'm having a vexing problem with global variables in Python. ... for tmp in foo: ...
    (comp.lang.python)