Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; '__init__': 0.09; 'constructor': 0.09; 'method:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "'b',": 0.16; "('b',": 0.16; '@classmethod': 0.16; '__new__': 0.16; 'constructs': 0.16; 'dict': 0.16; 'subject:class': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'memory': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; '[1]': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'steven': 0.31; 'class': 0.32; 'regular': 0.32; 'url:python': 0.33; 'basic': 0.35; 'agree': 0.35; 'possible.': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'c++': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'example,': 0.37; 'called': 0.40; 'url:mail': 0.40; 'is.': 0.60; 'most': 0.60; 'further': 0.61; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'details,': 0.68; 'yes': 0.68; 'answer.': 0.68; 'default': 0.69; 'obvious': 0.74; 'good,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=tFqAGbq+NVxfhEqu+Oo32uoxiybTBtTzRNL1qa2/PDk=; b=WHZO3j1J5ZCrh/k+SiTr/BgDqHv7PxdEsX1e0Qhx4CQpRjqHbwFEqzOLT0Al/3Vtyy 7R3HsaF1hTWACvKzIjGcTt4XWj6nPjOyvhSIV2DzFOdoKTNxy1pvOOcZL9bSFrps2Mbl 5p3HrEPv56iIYXOmF/dgqeOmYpYIil1mYsy7h5PLixMXVbXfGxQOBKE/83rsZVVzGHxg vY9mAofGj6sbI55AF3E0uzVw4syfovq0TR54rNUikSB4U/DiQgidwA23PkwBaUoxpvj3 X+6yVepMejrQ+z5xd85e0oX3SyiQhHHMouZb5uyEeIvRW0SMvzGEqYrSBK5pvJ65odiT lrRg== MIME-Version: 1.0 X-Received: by 10.221.56.194 with SMTP id wd2mr14125481vcb.7.1376561397465; Thu, 15 Aug 2013 03:09:57 -0700 (PDT) In-Reply-To: <520c81f6$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <520b913f$0$13995$426a74cc@news.free.fr> <520c81f6$0$29885$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 15 Aug 2013 11:09:57 +0100 Subject: Re: many constructors in a class? From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=001a11337ef61b912a04e3f9adfa Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 121 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376561401 news.xs4all.nl 15923 [2001:888:2000:d::a6]:53318 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52546 --001a11337ef61b912a04e3f9adfa Content-Type: text/plain; charset=ISO-8859-1 I agree with Steven here. classmethod is the best practise, most practical, readable, future-proof, one obvious way to do it. On 15 Aug 2013 08:29, "Steven D'Aprano" wrote: > On Wed, 14 Aug 2013 14:16:31 +0000, climb65 wrote: > > > Hello, > > > > here is a small basic question : > > > > Is it possible to have more than one constructor (__init__ function) in > > a class? For instance, to create an object with 2 different ways? If my > > memory is good, I think that with C++ it is possible. > > > > Thanks for your answer. > > Yes it is. The built-in type dict is a good example, there is the regular > default constructor[1] that you can call like this: > > dict([('a', 100), ('b', 200)], spam=1, ham=2, eggs=3) > > > Plus there is an alternative constructor that you can call like this: > > dict.fromkeys(['a', 'b', 'spam', 'ham', 'eggs']) > > > The way to create an alternative constructor is to use a class method: > > > def MyDict(dict): > @classmethod > def fromkeys(cls, keys): > ... > > > If you need further details, please ask. > > > > > [1] The constructor is __new__, not __init__. __init__ is called to > initialise the instance after __new__ constructs it. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > --001a11337ef61b912a04e3f9adfa Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

I agree with Steven here.

classmethod is the best practise, most practical, readable, = future-proof, one obvious way to do it.

On 15 Aug 2013 08:29, "Steven D'Aprano&= quot; <steve@pearwood.info>= ; wrote:
On Wed, 14 Aug 2013 14:16:31 +0000, climb65 wrote:

> Hello,
>
> here is a small basic question :
>
> Is it possible to have more than one constructor (__init__ function) i= n
> a class? For instance, to create an object with 2 different ways? If m= y
> memory is good, I think that with C++ it is possible.
>
> Thanks for your answer.

Yes it is. The built-in type dict is a good example, there is the regular default constructor[1] that you can call like this:

dict([('a', 100), ('b', 200)], spam=3D1, ham=3D2, eggs=3D3)=


Plus there is an alternative constructor that you can call like this:

dict.fromkeys(['a', 'b', 'spam', 'ham', = 9;eggs'])


The way to create an alternative constructor is to use a class method:


def MyDict(dict):
=A0 =A0 @classmethod
=A0 =A0 def fromkeys(cls, keys):
=A0 =A0 =A0 =A0 ...


If you need further details, please ask.




[1] The constructor is __new__, not __init__. __init__ is called to
initialise the instance after __new__ constructs it.


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
--001a11337ef61b912a04e3f9adfa--