Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52530

Re: many constructors in a class?

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 <beth.mcnany@gmail.com>
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; '&gt;&gt;&gt;': 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 <beth.mcnany@gmail.com>
Date Wed, 14 Aug 2013 10:40:03 -0400
Subject Re: many constructors in a class?
To climb65 <climb65@laposte.net>
Content-Type multipart/alternative; boundary=047d7bdc194e69367004e3e956e7
X-Mailman-Approved-At Wed, 14 Aug 2013 20:01:22 +0200
Cc Python Mailing List <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.585.1376503283.1251.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Wed, 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:

>>> class Test:
...  def __init__(self):
...    print "first init"
...  def __init__(self, arg):
...    print "init with arg"
...
>>> a = Test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

many constructors in a class? climb65 <climb65@laposte.net> - 2013-08-14 14:16 +0000
  Re: many constructors in a class? Phil Le Bienheureux <phil.le.bienheureux@gmail.com> - 2013-08-14 16:46 +0200
  Re: many constructors in a class? duncan smith <buzzard@invalid.invalid> - 2013-08-14 16:07 +0100
  Re: many constructors in a class? Beth McNany <beth.mcnany@gmail.com> - 2013-08-14 10:40 -0400
  Re: many constructors in a class? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-14 18:40 -0400
  Re: many constructors in a class? Steven D'Aprano <steve@pearwood.info> - 2013-08-15 07:23 +0000
    Re: many constructors in a class? Fábio Santos <fabiosantosart@gmail.com> - 2013-08-15 11:09 +0100
    Re: many constructors in a class? Roy Smith <roy@panix.com> - 2013-08-15 09:00 -0400

csiph-web