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


Groups > comp.lang.python > #19818

Re: multiple constructor __init__

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'python': 0.08; 'value:': 0.09; 'def': 0.13; 'argument': 0.15; 'cc:addr:python-list': 0.15; 'instantiate': 0.16; 'obj1': 0.16; 'obj2': 0.16; 'parameter,': 0.16; 'url:riverbankcomputing': 0.16; 'wrote:': 0.16; 'idea?': 0.18; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'feb': 0.22; 'header:In-Reply-To:1': 0.22; 'specify': 0.24; 'cc:2**0': 0.25; 'pm,': 0.26; 'all,': 0.27; 'received:209.85.220': 0.27; 'message- id:@mail.gmail.com': 0.28; 'example': 0.28; 'cc:addr:python.org': 0.29; 'second': 0.29; 'class': 0.29; '3.x': 0.30; 'parent': 0.30; 'chris': 0.30; 'error': 0.30; 'version': 0.31; 'suggested': 0.32; 'thu,': 0.32; 'instead': 0.33; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'skip:_ 10': 0.37; 'some': 0.38; 'received:209.85': 0.38; 'url:docs': 0.39; 'received:209': 0.39; 'subject:: ': 0.39; 'url:uk': 0.40; 'more': 0.61; 'url:co': 0.63; 'believe': 0.65; 'supply': 0.68; 'emmanuel': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=M+Z0DVsf2ep4FvXniZVQYSbuW/0fBvNKeHcWDwB7cKk=; b=PJBB35N6him/JcfLw1muswr1sVS9vEKnsC8HiT3R3h0agaEKERU2mjXLWWkYqCksOx ZoN49K1DfHo3qm7KSpcS1nx+oDT1Jk2vaYpDRP822Bf5o5y29rCDosDRYaGDawJN47nS Qbo/x+jCWuGt9nXTR+/YRAXQNJqqSKRuuFolM=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <CACB6ZmB9ofRz+ZAjqbbnwm2GfL9Vwth+jrz2mAQoMBp6-x5o2A@mail.gmail.com>
References <CACB6ZmB9ofRz+ZAjqbbnwm2GfL9Vwth+jrz2mAQoMBp6-x5o2A@mail.gmail.com>
Date Thu, 2 Feb 2012 17:24:23 -0800
X-Google-Sender-Auth rkDbRABLDbt-I5Ls64NEBBLurTI
Subject Re: multiple constructor __init__
From Chris Rebert <clp2@rebertia.com>
To Emmanuel Mayssat <emayssat@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.5386.1328232270.27778.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328232270 news.xs4all.nl 6843 [2001:888:2000:d::a6]:33484
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19818

Show key headers only | View raw


On Thu, Feb 2, 2012 at 5:09 PM, Emmanuel Mayssat <emayssat@gmail.com> wrote:
> Hello all,
>
> I would like to instantiate my class as follow
>
> QObject(<param1>, <parent>)
> QObject(<parent>)
>
> an example would be
> http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmenu.html
>
> How can I do this without have to specify parent=<parent> in the second
> version
> (I always need to supply the parent parameter, but I would like to supply it
> last)
>
> I have read this
> http://stackoverflow.com/questions/356718/how-to-handle-constructors-or-methods-with-a-different-set-or-type-of-argument
> but all the suggested methods do not work as I want
>
> Any idea?

I believe you can approximate that using a keyword-only argument
without a default value:

# Untested!
# Requires Python 3.x
class QObject(object):
    def __init__(self, param1=some_default, *, parent):
        # …

obj1 = QObject(parent=daddy)
obj2 = QObject(arg1, parent=daddy)

obj3 = QObject(daddy) # ERROR
obj4 = QObject(arg1, daddy) # ERROR
obj5 = QObject() # ERROR

Cheers,
Chris
--
Using names instead of some arbitrary ordering makes much more sense.
http://rebertia.com

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


Thread

Re: multiple constructor __init__ Chris Rebert <clp2@rebertia.com> - 2012-02-02 17:24 -0800

csiph-web