CONSTRUCT - Python's way of Ruby's "alias_method"

Ilias Lazaridis ilias at lazaridis.com
Sat Jun 10 14:41:01 EDT 2006


Maric Michaud wrote:
> Le Vendredi 09 Juin 2006 20:06, Ilias Lazaridis a écrit :
>> the code below works, but has the limitation that I cannot import the
>> syncdb_hook within "django.core.management".
> 
> In [4]: from b import CONS
> 
> In [5]: import b
> 
> In [6]: b.CONS = 3
> 
> In [7]: CONS
> Out[7]: 5
> 
> In [8]: from b import CONS
> 
> In [9]: CONS
> Out[9]: 3
> 
> So, if you change one module name, a function or a class or a constant, you 
> must do it before it is imported, or you must reload modules using it. But 
> either are not always possible, and the later is not what you want to achieve 
> here as it will re-execute all initialisation code in those modules.
> 
> But think of that, a function is hopefully an  object in python, hmmm :
> 
> In [1]: from temp import func
> 
> In [2]: func(5)
> Out[2]: 5
> 
> In [3]: def g(s) : return s*2
>    ...:
> 
> In [4]: func.func_code = g.func_code
> 
> In [5]: func(5)
> Out[5]: 10
> 
> hey, that should work !

Great Construct! Much flexibility!

I'll try the implementation tomorrow.

-

The actual Versions of the hooks can be found here:

http://case.lazaridis.com/browser/django/rework/syncdb_hook.py?rev=7
http://case.lazaridis.com/browser/django/rework/startproject_hook.py?rev=13

This construct has helped to simplify nearly all simplification goals:

http://case.lazaridis.com/wiki/DjangoSchemaEvolution

.

-- 
http://lazaridis.com



More information about the Python-list mailing list