mutually exclusive arguments to a constructor

Adam Funk a24061 at ducksburg.com
Sat Dec 31 15:55:09 EST 2011


On 2011-12-30, Günther Dietrich wrote:

> Adam Funk <a24061 at ducksburg.com> wrote:
>
>>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.
...
> You can determine the type of the input data by using isinstance() and 
> take the appropriate actions depending on this decision:
>
>>>> class MyClass(object):
> ...     def __init__(self, input_data):
> ...         if isinstance(input_data, basestring):
> ...             print "Do actions for string type input"
> ...         elif isinstance(input_data, float):
> ...             print "Do actions for float type input"
> ...     def get_output_data(self):
> ...         return "output data"

Aha, I think I like this approach best, partly because I realized
after writing my post that it might also be good to accept strings
representing "pure" angles (e.g., "65d30m").  So I think I'll use
isinstance *and then* check the input string against some regexes to
determine whether it's in traditional surveying notation or trig
notation in DMS.


-- 
The generation of random numbers is too important to be left to
chance.                                     [Robert R. Coveyou]



More information about the Python-list mailing list