s713221 at student.gu.edu.au
Fri Aug 10 12:01:57 CEST 2001
Alex Martelli wrote:
> "David Smith" <drs at labs.agilent.com> wrote in message
> news:3B72DCBA.C99465BC at labs.agilent.com...
> > class I have at hand, __init__ does some real work, which I want to
> > bypass -- I want to clone the results of that work. I don't want to
> > Is there a way for __copy__ to create a bare object of the same class,
> > which it can proceed to populate?
> Piece of cake:
> class Fleep:
> def __init__(self, x, y, z):
> print 'lots',x,'of',y,'work',z
> def __copy__(self):
> class Temp: pass
> newbie = Temp()
> print "very little work"
And adding onto that an automatic copy of the instance's __dict__ -->
>>> import copy
>>> class Fleep:
... def __init__(self,x,y,z):
... print 'lots',x,'of',y,'work',z
... self.x = x
... self.y = y
... self.z = z
... def __copy__(self):
... class Temp: pass
... newbie = Temp()
... newbie.__class__ = self.__class__
... newbie.__dict__ = copy.deepcopy(self.__dict__)
... print "very little work"
... return newbie
I LOVE this language. Alex, you're empty class trick just made it into
my private python scrap-book.
Joal Heagney is: _____ _____
/\ _ __ __ _ | | _ ___ |
/__\|\ || ||__ |\ || |___|/_\|___] |
/ \ \_||__ ||___| \_|! | | \ \ !
More information about the Python-list