Path: csiph.com!x330-a1.tempe.blueboxinc.net!hal.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.01; 'suppose': 0.05; 'method,': 0.07; 'python': 0.08; 'none)': 0.09; 'def': 0.13; 'argument': 0.15; '__init__': 0.16; 'above?': 0.16; 'calculation': 0.16; 'measured': 0.16; 'posted:': 0.16; 'arguments': 0.18; 'java,': 0.19; 'header:In-Reply-To:1': 0.22; 'indicating': 0.23; 'optional': 0.23; 'values.': 0.23; 'string': 0.24; 'sender:addr:gmail.com': 0.25; 'creating': 0.25; "i'm": 0.26; 'message-id:@mail.gmail.com': 0.28; 'print': 0.29; 'class': 0.29; 'correct': 0.29; 'outlined': 0.30; 'usually': 0.31; "i've": 0.31; 'represents': 0.32; 'received:209.85.160': 0.33; 'to:addr:python- list': 0.34; 'skip:# 10': 0.34; 'skip:@ 10': 0.34; 'received:209.85.160.46': 0.35; 'received:mail- pw0-f46.google.com': 0.35; 'similar': 0.36; 'heading': 0.37; 'two': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'answers': 0.38; 'received:209.85': 0.38; 'created': 0.38; 'either': 0.39; 'or,': 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'double': 0.61; 'taking': 0.66; 'traditional': 0.68; 'trigonometry': 0.84; '\xa0but': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; bh=L8CTRFgfT/OV/B6IaRATTfeFPr04HHeOdq7EESQb0Fo=; b=smuFRBPs5iwe67g3R46EXyN/9EFlaGzpFJsNi6EvfSwm+HzF+KF6neY0TsgsACxkw3 aHnJ45d3ctAw9YE4Qa9autyA+nTw7hm/6UgMyGRIMZ5tEL9pcP+LQwSIaIg2ARY/0HUt lUVzLsBOoBrw+HRei+lUwVyoyVm2sXOfgLV0E= MIME-Version: 1.0 Sender: jsf80238@gmail.com In-Reply-To: References: Date: Fri, 30 Dec 2011 21:18:29 +0000 X-Google-Sender-Auth: iHIltQL5LxLjZnBRJbBZzBuxrx0 Subject: Re: mutually exclusive arguments to a constructor From: Jason Friedman To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325279912 news.xs4all.nl 6987 [2001:888:2000:d::a6]:58113 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18231 > 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). =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. =A0But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. =A0What's the correct way to > deal with a situation like the one I've outlined above? Similar to other answers already posted: #!/usr/bin/env python class azimuth: def __init__(self, bearing, heading): self.bearing =3D bearing self.heading =3D heading if not bearing: self.bearing =3D 30.5 # or, realistically, a calculation based on the heading if not heading: self.heading =3D "N..." # or, realistically, a calculation based on the bearing @staticmethod def getBearingInstance(bearing): return azimuth(bearing, None) @staticmethod def getHeadingInstance(heading): return azimuth(None, heading) azimuth1 =3D azimuth.getBearingInstance("N24d30mE") print azimuth1.heading azimuth2 =3D azimuth.getHeadingInstance(30) print azimuth2.bearing