Copy constructors

Roman Suzi rnd at onego.ru
Mon Aug 13 03:27:42 EDT 2001


Hello,

I am against PEP 252 in its present form!

It makes OO in Python too complex without obvious necessity.

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.

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:

--------------------------------------------------------

def do_fly(self):
  return "I am flying."
def do_swim(self):
  return "I am swiming."
def do_sing(self):
  return "I am singing."

class Everybody:
  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"""
    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.

-----------------------------------------------------------

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


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!!!

The summary of proposal is to leave things as they are, but to introduce
__builtin__ namespace for optimized methods.

(Name __builtin__ could be different)


Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 







More information about the Python-list mailing list