Terry Reedy wrote:
> Not to me.  You are using the module as a singleton class. The
> alternative is to write a class, make the functions methods, and
> instantiate the class.  If that instance must be a singleton, more work
> is required.  If multiple instances make sense, you can go the class
> route when you need to.

I've thought of this too, but it turns out those functions are related to each
other very loosely. They do a lot of completely different things independently
and have just one common global variable to share. IMHO it would reduce the
readability of code because the role of this class is very unclear. Anyway,
"readability" is mostly a subjective matter and I think what you pointed out is
a good idea in general.

>> For example:
>>
>>     @global_injected("SPAM")
>>     def foo():
>>         ... ...
>>
>> will have the same effect as
>>
>>     def foo():
>>         global SPAM
>>         ... ...
> 
> More keystrokes for no gain.

That's intended. I want these "evil" (or "foolish and senseless") functions to
"stand out" in the sourcecode. They have their evil marks right before the
definition, not buried in the function body.

> Decorators are usally intended to wrap the input function with another
> function.  But they can be used to modify the input function and return
> it altered.  The latter is what you are proposing.
> 
> You of course have to use SPAM as a local variable.  Then you might be
> able to write an implementation-specific convert_to_global(name)
> function that would rewrite the code part of the code object of the
> function to what it would have been had you been sensible and used the
> 'global' directive.  This would mean finding the code that loads and
> stores 'SPAM' as a local variable and convert is to code that loads and
> stores it as a global variable.  If the replacement takes more bytes
> than the original, then the jump offsets all have to be fixed.  Also
> needing fixing would be the line number table that is used to match code
> units to lines in the Python code for error tracebacks.
> 
> An alterntive would be to de-compile the code, insert the global
> directive, and recompile.  A non-decorator alternative would be to write
> a function with two parameters that takes a tuple of names and the code
> quoted as a string as arguments.  It would insert the global statement
> into the string, execute it, and return the function.

Thank you for your elaborate explanation :)

Regards,
Cong.

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to