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


Groups > comp.lang.python > #18231

Re: mutually exclusive arguments to a constructor

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 <jsf80238@gmail.com>
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 <g6k1t8xg0a.ln2@news.ducksburg.com>
References <g6k1t8xg0a.ln2@news.ducksburg.com>
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 <jason@powerpull.net>
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 <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.4254.1325279912.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


> 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).  The 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.  But in Python, a
> class can have only one __init__ method, although it can have a lot of
> optional arguments with default values.  What'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 = bearing
        self.heading = heading
        if not bearing:
            self.bearing = 30.5 # or, realistically, a calculation
based on the heading
        if not heading:
            self.heading = "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 = azimuth.getBearingInstance("N24d30mE")
print azimuth1.heading

azimuth2 = azimuth.getHeadingInstance(30)
print azimuth2.bearing

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


Thread

mutually exclusive arguments to a constructor Adam Funk <a24061@ducksburg.com> - 2011-12-30 20:40 +0000
  Re: mutually exclusive arguments to a constructor "Günther Dietrich" <gd.usenet@spamfence.net> - 2011-12-30 22:00 +0100
    Re: mutually exclusive arguments to a constructor Adam Funk <a24061@ducksburg.com> - 2011-12-31 20:55 +0000
  Re: mutually exclusive arguments to a constructor Mel Wilson <mwilson@the-wire.com> - 2011-12-30 16:08 -0500
  Re: mutually exclusive arguments to a constructor Arnaud Delobelle <arnodel@gmail.com> - 2011-12-30 21:14 +0000
  Re: mutually exclusive arguments to a constructor Jason Friedman <jason@powerpull.net> - 2011-12-30 21:18 +0000
    Re: mutually exclusive arguments to a constructor Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-30 22:21 +0000
  Re: mutually exclusive arguments to a constructor Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-30 22:13 +0000
  Re: mutually exclusive arguments to a constructor Roy Smith <roy@panix.com> - 2011-12-30 18:24 -0500
    Re: mutually exclusive arguments to a constructor Chris Angelico <rosuav@gmail.com> - 2011-12-31 10:36 +1100
      Re: mutually exclusive arguments to a constructor Roy Smith <roy@panix.com> - 2011-12-30 18:39 -0500
        Re: mutually exclusive arguments to a constructor Chris Angelico <rosuav@gmail.com> - 2011-12-31 10:47 +1100
    Re: mutually exclusive arguments to a constructor Adam Funk <a24061@ducksburg.com> - 2011-12-31 20:59 +0000

csiph-web