>From the usual source:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
My answer:
Armin suggests exec'ing in an environment and not in the outer
environment. That is what web2py does and in fact we already follow
much of the advice he gives us about about how to use exec. We just do
not follow the advice not to use exec. ;-)
Performance is not an issue because 1) the time to bytecompile is
often small compared to the database IO time and because 2) web2py
allows you to bytecode compile apps anyway.
In web2py you cannot store instances of objects into session. While
Armin makes it tool like this is a web2py problem this is more complex
and general than that. In Python if you pickle and object that is
instance of a class defined in /path1/mymodule.py and then you
unpickle on a different installation where the class is defined in /
path2/mymodule.py you may run into problems. Because web2py does not
require installation (it is a feature) and has hot plug and play of
apps (another feature) we cannot pickle instances (the price we pay
for those features). To me it is worth it.
Once again Armin raises the issue of memory leaks. Unfortunately
CPython does not use a proper garbage collection but uses reference
counting. It suffers from memory leaks when circular references are
created. This is not a web2py specific problem. This is a problem with
any Python program. For example the following code
class A: pass
a=A()
a.b=a
causes a memory leak in ANY python program.
I agree with Armin that in the case of exec (and web2py uses exec),
the self references are created whether you want them or not. That
means one should not define classes in Models and Controllers. I have
issued that warning already to our users and, I have never seen this
being a problem in practice.
Nothing in life is perfect and every design decision has a tradeoff.
What would really be useful to us is an example of how to overcome
that problem by showing how to break those implicit self references.
Massimo