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


Groups > comp.lang.python > #18230

Re: mutually exclusive arguments to a constructor

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 <arnodel@gmail.com>
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 <g6k1t8xg0a.ln2@news.ducksburg.com>
References <g6k1t8xg0a.ln2@news.ducksburg.com>
Date Fri, 30 Dec 2011 21:14:05 +0000
Subject Re: mutually exclusive arguments to a constructor
From Arnaud Delobelle <arnodel@gmail.com>
To Adam Funk <a24061@ducksburg.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.4253.1325279649.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 30 December 2011 20:40, Adam Funk <a24061@ducksburg.com> 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).  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?

(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 = 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 = Bearing(65.5)

or

b = 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 = float(angle)
        self.angle = angle

Now you can do:

b = Bearing(65.5)

or

b = 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

-- 
Arnaud

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