Copy constructors
Guido van Rossum
guido at python.org
Mon Aug 13 07:18:01 EDT 2001
> I am against PEP 252 in its present form!
The problem you indicate below has to do with PEP 253, not PEP 252.
> It makes OO in Python too complex without obvious necessity.
Huh? I've always thought that the inability to subclass built-in
classes made OO more complex than necessary, and that's what these
PEPs (together with PEP 254, which won't be part of Python 2.2) are
removing.
> One of the cool features of Python I am glad it has is the ability
> to change class attributes on the fly (I've already posted it to
> Guido some time ago (maybe a year ago), specifically asking NOT TO
> CHANGE this Python ability.
You will still be able to do that, by putting __dynamic__ = 1 in your
base class (it is automatically inherited).
> No API will make things as simple as they are shown below:
>
> This is my test if the changes to the Python are appropriate
> for me:
[snip]
Here's a version that works with Python 2.2. I've commented lines I
changed or added. The "__dynamic__ = 1" line must be added because
new-style classes will be immutable by default (note: classic classes
are not affected in Python 2.2). The try/except statement with the
call to object.__getattr__() is because new-style classes allow you to
overload __getattr__ for all attribute accesses -- classic classes
only call __getattr__ when "normal" attribute access fails, which is
less flexible.
----------------------------------------------------------------------
def do_fly(self):
return "I am flying."
def do_swim(self):
return "I am swimming."
def do_sing(self):
return "I am singing."
class Everybody(object):
__dynamic__ = 1 # Added
def i_am(self):
return "I am %s." % self.__class__.__name__
def __getattr__(self, attname):
"""This is added only for the text below to be "nice",
in reality it is not needed"""
try: # Added
return object.__getattr__(self, attname) # Added
except AttributeError: # Added
def cant_do(self=None, action=attname):
return "I can't %s." % action
return cant_do
class Fish(Everybody):
swim = do_swim
class Mermaid(Everybody):
sing = do_sing
swim = do_swim
class Bird(Everybody):
fly = do_fly
sing = do_sing
class Man(Everybody):
sing = do_sing
f = Fish(); r = Mermaid(); b = Bird(); m = Man()
print f.i_am(), f.swim(), f.sing(), f.fly()
print r.i_am(), r.swim(), r.sing(), r.fly()
print b.i_am(), b.swim(), b.sing(), b.fly()
print m.i_am(), m.swim(), m.sing(), m.fly()
print "Man learned to swim."
Man.swim = do_swim
print m.i_am(), m.swim(), m.sing(), m.fly()
> -----------------------------------------------------------
>
> I am Fish. I am swiming. I can't sing. I can't fly.
> I am Mermaid. I am swiming. I am singing. I can't fly.
> I am Bird. I can't swim. I am singing. I am flying.
> I am Man. I can't swim. I am singing. I can't fly.
> Man learned to swim.
> I am Man. I am swiming. I am singing. I can't fly.
>
> -----------------------------------------------------------
The above code gives the identical same output under Python 2.2. Try
it!
> So I fully understand Glyph's concerns!
>
> As I said already, PEP252 makes things too complex. Probably, there is a
> more obvious way to make builtin classes inheritable and type==class
> things.
>
> For example, by using __builtin__ trick:
>
> class MyNumber(integer):
>
> def myadd(self, x, y):
> return x*y
>
> __builtin__.add = myadd
Sorry -- I have no idea what you are proposing here. What is
'integer'? What is the meaning of '__builtin__'? It can't be the
__builtin__ module. But then what is it?
> The essence of the idea is to gather builtin (C) methods into special
> namespace: __builtin__, the same way it is done with builtin fuctions.
> This will allow Python programmers to remember less details and
> will give shadowing for free!!!
Three exclamation points do not an argument make.
> The summary of proposal is to leave things as they are, but to introduce
> __builtin__ namespace for optimized methods.
>
> (Name __builtin__ could be different)
Roman, forgive me for saying so, but I think you're way out of your
league here. Rather than trying to propose your own design, please
study my example above and see if you're still unhappy.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-list
mailing list