copy construtor question

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Aug 29 06:48:36 CEST 2009


En Fri, 28 Aug 2009 21:47:51 -0300, Chris Rebert <clp2 at rebertia.com>  
escribió:
> On Fri, Aug 28, 2009 at 11:38 AM, xiaosong xia<xiaosongxia at yahoo.com>  
> wrote:
>>
>> 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)

For simple enough cases, the copy and deepcopy functions in the copy  
module are just fine:
http://docs.python.org/library/copy.html

-- 
Gabriel Genellina




More information about the Python-list mailing list