Path: csiph.com!x330-a1.tempe.blueboxinc.net!hal.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'else:': 0.03; '(using': 0.05; 'below)': 0.05; 'suppose': 0.05; 'method,': 0.07; 'python': 0.08; 'builtin': 0.09; 'types:': 0.09; 'def': 0.13; 'received:209.85.214.174': 0.13; 'argument': 0.15; '__init__': 0.16; 'above?': 0.16; 'adam': 0.16; 'measured': 0.16; 'str):': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'arguments': 0.18; 'java,': 0.19; '(which': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.20; 'java': 0.21; 'header:In- Reply-To:1': 0.22; '----------': 0.23; 'indicating': 0.23; 'optional': 0.23; 'values.': 0.23; 'string': 0.24; 'cc:2**0': 0.24; 'creating': 0.25; 'obviously': 0.25; "i'm": 0.26; 'fact': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'class': 0.29; 'correct': 0.29; 'none,': 0.30; 'outlined': 0.30; 'skip:b 30': 0.31; 'usually': 0.31; "i've": 0.31; 'represents': 0.32; 'received:209.85.214': 0.32; 'here,': 0.35; 'test': 0.35; 'question': 0.36; 'two': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'could': 0.37; 'using': 0.38; 'received:209.85': 0.38; 'created': 0.38; 'i.e.': 0.39; 'either': 0.39; 'received:209': 0.40; 'more': 0.61; 'type': 0.61; '2011': 0.61; 'your': 0.61; 'double': 0.61; 'here': 0.65; 'taking': 0.66; 'natural': 0.66; 'traditional': 0.68; 'funk': 0.84; 'me).': 0.84; 'trigonometry': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=u2uAd/ec/g8sdkLsHFaCU+NkIdj4NvJQ0Zu6bsujntE=; b=bNyyOqQUCDESzgAV/eg6zmaC1mRBGaxMZw1TN8yYbZ8XAfvTmXedAfrDXb3nkOmdZ7 JDXXmleUPSfYgjTKOl9bPZES4nFx1O4zyp3j8YwB7QeTnFivK32URH7MAmPzPiQhmit+ uqUgsZAgjMR6+PBvuP7ZJYf63GtvMJ1qPs7I4= MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 30 Dec 2011 21:14:05 +0000 Subject: Re: mutually exclusive arguments to a constructor From: Arnaud Delobelle To: Adam Funk 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325279649 news.xs4all.nl 6939 [2001:888:2000:d::a6]:48419 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18230 On 30 December 2011 20:40, Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). =C2=A0The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. =C2=A0But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. =C2=A0What's the correct way to > deal with a situation like the one I've outlined above? (Using Python 3 below) Method 1 ---------- Your __init__ method could take the angle as an argument (which seems the most natural to me). Then you could have a class method that takes the string i.e. class Bearing: def __init__(self, angle): self.angle =3D angle # or whatever your internal reprsentation is @classmethod def fromstring(cls, string): # Here, work out the angle from the string return cls(angle) So you can do: b =3D Bearing(65.5) or b =3D Bearing.fromstring("N24d30mE") Method 2 ---------- You can test the type of the argument of the __init__ method class Bearing: def __init__(self, arg): if isinstance(arg, str): # here calculate the value of angle else: angle =3D float(angle) self.angle =3D angle Now you can do: b =3D Bearing(65.5) or b =3D Bearing("N24d30mE") Both methods are used for builtin types: >>> int('12') 12 >>> int(12.5) 12 >>> dict([(1, 2), (3, 4)]) {1: 2, 3: 4} >>> dict.fromkeys([1, 2]) {1: None, 2: None} HTH --=20 Arnaud