Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'class,': 0.07; '__init__': 0.09; 'arguments': 0.09; 'arguments,': 0.09; 'constructor': 0.09; 'emulate': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'arg):': 0.16; 'given)': 0.16; 'optional': 0.16; 'subject:class': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'memory': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; '>>>': 0.24; 'instance,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'basic': 0.35; 'possible.': 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'c++': 0.36; 'method': 0.36; 'thanks': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'skip:& 10': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'does': 0.39; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'answer.': 0.68; 'covers': 0.68; 'behavior': 0.77; '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:from:date:message-id:subject:to :cc:content-type; bh=6EoLI947wLwkhv9SDwtkCJMTT9AmxMTrovHPTWJhxbU=; b=ovhcGCqUGxHPKmBdrDVhRlgWpYGKHaqKQvDY6Y08VVBGDRRAgb1pNdb1rjVbqi36t3 Zn2yXtlkxYhLA6uRDychCwK0HCrMfVjTy7lksK6GpYEC7BOsNqtTN2xS26UHwQeYzV5G 9ZNW9CWB8CpyAqRgwXt/l/DJ4I+vLXbqfXxrdSaxh10XEl+OB3icdthYEMDiUxFEXWtK xR+KpUuT/MepcPWrWWPGleLl81lQPplXaqtwMy6whgZVuu1SY2K9kIbDU2JSWXQGqJZT o2Tq6GECqY9Ud/vmfLBGZjqP5N4GTgfzSAE+I6BYiNozlZxVuUxj+qEngKE8ere4JcnV 4fWg== X-Received: by 10.50.11.46 with SMTP id n14mr2325903igb.15.1376491223461; Wed, 14 Aug 2013 07:40:23 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <520b913f$0$13995$426a74cc@news.free.fr> References: <520b913f$0$13995$426a74cc@news.free.fr> From: Beth McNany Date: Wed, 14 Aug 2013 10:40:03 -0400 Subject: Re: many constructors in a class? To: climb65 Content-Type: multipart/alternative; boundary=047d7bdc194e69367004e3e956e7 X-Mailman-Approved-At: Wed, 14 Aug 2013 20:01:22 +0200 Cc: Python Mailing List 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376503283 news.xs4all.nl 15976 [2001:888:2000:d::a6]:51246 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52530 --047d7bdc194e69367004e3e956e7 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Aug 14, 2013 at 10:16 AM, 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. > > No, Python does not allow method overloading: >>> class Test: ... def __init__(self): ... print "first init" ... def __init__(self, arg): ... print "init with arg" ... >>> a = Test() Traceback (most recent call last): File "", line 1, in TypeError: __init__() takes exactly 2 arguments (1 given) No error on actually writing the class, but only the last __init__ is kept. You could, however, emulate that behavior with optional arguments, or something more sophisticated as the need may be. This stackoverflow question covers a few alternatives: http://stackoverflow.com/questions/6434482/python-function-overloading --047d7bdc194e69367004e3e956e7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On W= ed, Aug 14, 2013 at 10:16 AM, climb65 <climb65@laposte.net> 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.


No, Python does not allow method overloading:

>>&g= t; class Test:
...=A0 def __init__(self):
...=A0=A0=A0 print "fi= rst init"
...=A0 def __init__(self, arg):
...=A0=A0=A0 print "init with arg&q= uot;
...
>>> a =3D Test()
Traceback (most recent call las= t):
=A0 File "<stdin>", line 1, in <module>
Typ= eError: __init__() takes exactly 2 arguments (1 given)

No error on actually writing the class= , but only the last __init__ is kept.=A0 You could, however, emulate that b= ehavior with optional arguments, or something more sophisticated as the nee= d may be.=A0 This stackoverflow question covers a few alternatives: http://stackoverflow.com/questions/6434482/python-functi= on-overloading
--047d7bdc194e69367004e3e956e7--