Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18235
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: mutually exclusive arguments to a constructor |
| Date | 2011-12-30 18:24 -0500 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-C73AC0.18242330122011@news.panix.com> (permalink) |
| References | <g6k1t8xg0a.ln2@news.ducksburg.com> |
In article <g6k1t8xg0a.ln2@news.ducksburg.com>,
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).
There's two ways to do this.
One would be to have the __init__ method switch on the type of its
argument:
def __init__(self, bearing_or_azimuth):
if isinstance(bearing_or_azimuth, basestring):
# do the bearing thing
else:
# do the azimuth thing
I suspect many people would consider that unpythonic. The other way
would be what, in the C++/Java world, would be called the "named
constructor idiom". Just write two factory functions:
class DirectionIndicatingThingie:
@staticmethod
def from_bearing(cls, bearing):
dit = DirectionIndicatingThingie()
dit.direction = whatever
return dit
and likewise for from_azimuth()
"But!", some C++/Java type bondage addicts might cry, "there's nothing
to prevent somebody from creating a DirectionIndicatingThingie directly,
bypassing the factory functions. There's no way to make the constructor
private!". To which the free-willed pythonistas would respond, "If it
hurts when you do that, don't do that".
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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