copy construtor question

Chris Rebert clp2 at
Sat Aug 29 02:47:51 CEST 2009

On Fri, Aug 28, 2009 at 11:38 AM, xiaosong xia<xiaosongxia at> 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
(, 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.

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


More information about the Python-list mailing list