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: 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: References: Date: Thu, 2 Feb 2012 17:24:23 -0800 X-Google-Sender-Auth: rkDbRABLDbt-I5Ls64NEBBLurTI Subject: Re: multiple constructor __init__ From: Chris Rebert To: Emmanuel Mayssat 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Feb 2, 2012 at 5:09 PM, Emmanuel Mayssat wrote= : > Hello all, > > I would like to instantiate my class as follow > > QObject(, ) > QObject() > > 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=3D 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-m= ethods-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=3Dsome_default, *, parent): # =E2=80=A6 obj1 =3D QObject(parent=3Ddaddy) obj2 =3D QObject(arg1, parent=3Ddaddy) obj3 =3D QObject(daddy) # ERROR obj4 =3D QObject(arg1, daddy) # ERROR obj5 =3D QObject() # ERROR Cheers, Chris -- Using names instead of some arbitrary ordering makes much more sense. http://rebertia.com