dynamic-on-demand classes Re: Copy constructors

Guido van Rossum guido at python.org
Mon Aug 13 07:28:31 EDT 2001


> The thought is simple: let all classes be static UNTIL operation is
> requested which need them to be dynamic. Then, __dynamic__ attribute
> appears = 1 and everybody is happy: those, who want to sacrifice
> speed for flexibility just let that operations happen. Those who use
> static classes just have them.

I wish it were so easy.  Consider this example:

    class C(object):
	def meth(self): return "hello world"

    class D(C):
	pass

    x = D()

    C.meth = lambda: "goodbye world"

    print x.meth()

Obviously you want this to print "goodbye world".  In order to do
this, the implementation must know that if C becomes a dynamic class
(by the assignment to C.meth), it must also make D (and all other
classes derived from C) dynamic.  This would require C to keep track
of all its derived classes.  That's not easy: it would require the use
of weak references.

Your proposal would also make it impossible to implement instances of
static classes different than instances of dynamic classes.  For a
static class, compile-time analysis (well, at least
class-definition-time analysis) can determine the exact set of
instance variables used by the methods defined by the class, and this
can be used to allocate the instance variables more efficiently than
using a dictionary.  But this can only be done when we know the class
will remain static over its lifetime.

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list