Global variables, Classes, inheritance

Claudio Grondi claudio.grondi at freenet.de
Fri Feb 3 20:57:49 EST 2006


DaveM wrote:
> Although I've programmed for fun - on and off - since the mid 70's, I'm
> definitely an OO (and specifically Python) beginner. 
> 
> My first question is about global variables. Are they, as I'm starting to
> suspect, a sin against God or just best avoided? Having got my current
> application working using them, I'm not sure whether I want to refactor it,
> but equally, I'd like to write the best code I can.
For me global variables in Python are not that much sin as global 
variables in another languages (as e.g. C/C++).
> 
> Secondly (and this is related),  if you have multiple instances of the same
> class, but need them all to have access to a dictionary created at run-time,
> is there a class way to this without calling the creation method multiple
> times? The best thought I've come up with is for the class instance to check
> its name and only if it's the first one do the dictionary creation, but I
> haven't tried it yet.
> 
> My third difficulty is with variables in a class. What's the difference
> between the following?:
> 
> class Foo:
>     i = 12345
>     ...
> 
> class Foo:
>     self.i = 12345
>     ...
> 
> class Foo:
>     def __init__(self):
>         self.i = 12345
>         ...
> 
> class Foo:
>     def __init(self):
>         i = 12345
>         ...
> 
> DaveM

Before some expert will enlighten you and provide appropriate links, you 
can just try to run what you have listed (e.g. to see what is broken) 
and watch the outcome.

Maybe it is a good idea to try to look at globals() and locals() to see 
what and when happens and take a look at the documentation to see what 
this functions are about.

Claudio



More information about the Python-list mailing list