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


Groups > comp.lang.python > #18230

Re: mutually exclusive arguments to a constructor

References <g6k1t8xg0a.ln2@news.ducksburg.com>
Date 2011-12-30 21:14 +0000
Subject Re: mutually exclusive arguments to a constructor
From Arnaud Delobelle <arnodel@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4253.1325279649.27778.python-list@python.org> (permalink)

Show all headers | 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