Refactoring Dilemma

George Sakkis george.sakkis at gmail.com
Sun Sep 10 15:10:28 EDT 2006


Kamilche wrote:
> '''
> I'm in the middle of a refactoring dilemma.
> I have several singletons that I'm turning into modules, for ease of
> access.
> The usual method is noted as 'Module 1' below.
> The new method is noted as 'Module 2'.
> Is there any reason NOT to do this that I may be unaware of?
> It's easier than remembering to declare global variables at the top of
> the function.
> '''
>
> # ----------- Module 1.py ------------
> # Normal module processing
> var = 0
>
> def MyRoutine():
>     global var
>     var = 1
>
> MyRoutine()
> print var
>
>
> # ----------- Module 2.py ------------
> # 'Self' module processing
> import sys
> var = 0
> self = sys.modules[__name__]
>
> def MyRoutine():
>     self.var = 1
>
> MyRoutine()
> print var


What's wrong with
<code>
def MyRoutine():
     return 1

var = MyRoutine()
</code>
?

George




More information about the Python-list mailing list