Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'none:': 0.07; 'constructor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'constructor.': 0.16; 'ignoring': 0.16; 'message-id:@4ax.com': 0.16; 'out"': 0.16; 'parameters,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:class': 0.16; 'aug': 0.22; 'instance,': 0.24; 'url:home': 0.24; 'question': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'parameters.': 0.31; 'skip:_ 10': 0.34; 'basic': 0.35; 'possible.': 0.35; 'but': 0.35; 'are,': 0.36; 'c++': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'received:76': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'more': 0.64; 'different': 0.65; 'default': 0.69; 'secret': 0.74; 'goal': 0.75; '"leave': 0.84; 'good,': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: many constructors in a class? Date: Wed, 14 Aug 2013 18:40:50 -0400 Organization: IISS Elusive Unicorn References: <520b913f$0$13995$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-105-23.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376520059 news.xs4all.nl 15891 [2001:888:2000:d::a6]:59796 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52534 On 14 Aug 2013 14:16:31 GMT, climb65 declaimed the following: >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. > Well... Secret Python-Fu -- __init__() is NOT a constructor. The constructor is __new__(); __init__() is just an initializer of whatever __new__() creates. Ignoring that detail... NO! If the goal is to be able to "leave out" some parameters, that is performed by setting default values for the missing parameters. def __init__(self, these, are, mandatory, but=None, this=None, is=None, optional=None): self.these = these self.are = are self.mandatory = mandatory if but is None: self.but = these + are else: self.but = but #etc. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/