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


Groups > comp.lang.python > #52520

Re: many constructors in a class?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <phil.le.bienheureux@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.058
X-Spam-Evidence '*H*': 0.88; '*S*': 0.00; '__init__': 0.09; 'constructor': 0.09; 'function,': 0.09; 'def': 0.12; 'afterwards': 0.16; 'subject:class': 0.16; '\xa0\xa0\xa0\xa0\xa0\xa0': 0.16; 'variable': 0.18; 'code,': 0.22; 'memory': 0.22; 'instance,': 0.24; 'question': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'url:python': 0.33; 'basic': 0.35; 'possible.': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'name': 0.63; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'answer.': 0.68; 'default': 0.69; 'good,': 0.91
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 :content-type; bh=h5WZZtad70yN3yJAotlHPyTXgKxmTekGe8u5dSAXnwY=; b=JhIOMa1KWl7qc2aub9CjJJfWT/P3gsQuk5Kcefo5Etjv+xIfc5g8WcujNqyJ+Z0oTt NK4TG+MNDXrHxzE274PdIoLnFFfOSmT+0ACSV+vA80hfAxMSiRYaD71PuVUV1pRmXFVG QbyRrGW5cDz4agLyeAnDR2LD2+Q5Oli/+MATjUbyldqtKQVg0+7s99sbA3HExuevti5V eRHwwbb7ge4S7tNJ4wiQCD8xXfBzK4nrCRp+LBo8OU60+C2vbFKm5qE5n1Fgq3pfHSDd UtoN2xGe2e18QHEE1nfF6TAaLqFFtOW5y/xESEHZAPVPjN4o+M28DqhoS57wuU1WH2G4 2lYw==
MIME-Version 1.0
X-Received by 10.66.170.138 with SMTP id am10mr10125307pac.51.1376491593217; Wed, 14 Aug 2013 07:46:33 -0700 (PDT)
In-Reply-To <520b913f$0$13995$426a74cc@news.free.fr>
References <520b913f$0$13995$426a74cc@news.free.fr>
Date Wed, 14 Aug 2013 16:46:33 +0200
Subject Re: many constructors in a class?
From Phil Le Bienheureux <phil.le.bienheureux@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=047d7bb0500073411b04e3e96c6a
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.578.1376491597.1251.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376491598 news.xs4all.nl 15964 [2001:888:2000:d::a6]:46514
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52520

Show key headers only | View raw


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

2013/8/14 climb65 <climb65@laposte.net>

> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



Hello,

You have to use default values in __init__ function, like :
def __init__( self, name = None ):
        self.name_ = name

and afterwards in your code, test variable :
if self.name_:
       do something...

Regards,
Phil.

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