On Fri, Aug 28, 2009 at 11:38 AM, xiaosong xia<xiaosong...@yahoo.com> wrote: > Hi all, > > I am trying to define a class with copy constructor as following: > > class test: > > def __init__(self, s=None): > > self=s
Python uses call-by-object (http://effbot.org/zone/call-by-object.htm), so reassigning `self` does not affect the object outside of the body of the method. It is (practically) impossible to reassign `self` in the way and with the effect you're trying to do; that is, you never change what `self` points to from the outside world's perspective, you can only modify the object it already points at. <snip> > 1. Can 'self ' be a class attribute? Doing this would make no sense. > 2. How to make a copy constructor? Since Python lacks overloading based on parameter types, generally one does it the other way around and instead provides a method that produces a copy of the object. For example: class Point(object): def __init__(self, x, y): self. x = x self.y = y def copy(self): return Point(self.x, self.y) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list