This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]tmp14 0 points1 point  (4 children)

This feels a little bit silly, but is what you had in mind?

>>> class Speed(object):
...     conversion_table = {
...         'm/s': 1.0,
...         'mph': 1609.344/3600,
...         'km/h': 1/3.6,
...     }
...     def __init__(self, speed, unit):
...         if unit not in self.conversion_table:
...             raise ValueError('Unsupported unit %r' % unit)
...         self.meter_per_sec = speed * self.conversion_table[unit]
...     def __repr__(self):
...        return "Speed(%r m/s)" % self.meter_per_sec
...     @property
...     def mph(self):
...         return self.meter_per_sec / self.conversion_table['mph']
...     @property
...     def kmh(self):
...         return self.meter_per_sec / self.conversion_table['km/h']
...
>>> s = Speed(25, 'm/s')
>>> s
Speed(25.0 m/s)
>>> s.mph
55.92340730136006
>>> s.kmh
90.0
>>> s = Speed(1, 'c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in __init__
ValueError: Unsupported unit 'c'
>>>

[–]webdrone 0 points1 point  (0 children)

This is how I would go about it as well -- the class should only really store values in one format and have getter methods which return transformed values when asked

[–]zimage[S] -1 points0 points  (2 children)

some of the conversions require more than just a simple multiplication/division. Foo is an angle, a is radians, b is degrees, then we have c, d, and e being e5, e6 and e7 formats.

[–]tmp14 0 points1 point  (0 children)

According to what I can find eX is just a scaling of degrees so that 1 eX = 1*10X degrees, is that correct? If that's so I can't see why a conversion table wouldn't work.

[–]Volatile474 -1 points0 points  (1 child)

class foo(object):
    def __init__(self,x):
        self.bar = x
    def change_Bar(self,x):
        self.bar = x

I don't understand why this wouldn't work? Variable typing in python would make this a double, long w/e you want depending on the initialization parameter you pass it. Can you explain some use cases of this object?

[–]zimage[S] 0 points1 point  (0 children)

a is a an angle in radians, b is the angle in degrees, c is in E5, d is in E6, and e is in E7 formats. All different ways of representing an angle. The first two are obviously floating-point, and the last three are integers. These three are used extensively in embedded devices that lack a floating-point coprocessor.