Re: mutually exclusive arguments to a constructor

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
def getBearingInstance(bearing):
return azimuth(bearing, None)
def getHeadingInstance(heading):
return azimuth(None, heading)

azimuth1 = azimuth.getBearingInstance("N24d30mE")
print azimuth1.heading

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

Relevant Pages

  • Re: Bearing and Course, differences?
    ... So as far as I understand it, bearing is a referenced heading to ... Course is an intended flight line, so if I plan to fly from a to b ... If there is a wind aloft ...
  • Re: Bearing and Course, differences?
    ... between magnetic/true course and magnetic/true bearing. ... So as far as I understand it, bearing is a referenced heading to ... required to fly direct) ...
  • Re: Garmin 76csx vs 60csx vs Vista Hcx vs ???
    ... Shortest way is to correct into the crosswind just enough to stop the bearing from changing; then your course over the ground will be a straight line directly to your nav point. ... If the GPS says bearing is 270, and I point the aeroplane so that the GPS says heading is 270, then I'll get there in a straight line. ... Do the Garmin chartplotters use this "wrong" meaning of heading too? ...
  • Re: Track vs Heading?
    ... The GPS cannot determine heading from the record of positions. ... Your bearing interpretation is correct, too, if you interpret "straight line" ... trails by walking on the trails and transferring the TRACK data via mapping ...
  • Re: Track, Bearing, Course, Heading
    ... from Ken Gibson? ... Track is the motion you are making over the ground. ... Bearing is the direction from you to someplace else. ... Heading is what direction the nose of the plane is pointing. ...