0
class....

    @property
    def degrees(self):
        hem, degs, mins, secs = self._correction()
        return degs

    @property
    def minutes(self):
        hem, degs, mins, secs = self._correction()
        return mins

    @property
    def seconds(self):
        hem, degs, mins, secs = self._correction()
        return secs

I was thinking that def could be truncated like:

@property
def hemisphere, degrees, minutes, seconds(self):
    hem, degs, mins, secs = self._correction()
    return hem, degs, mins, secs

so that when I call 'hemisphere' it will return hem values and so on.

PS. I know that my return statement in the latter was wrong. I hope you get my point.

azro
  • 53,056
  • 7
  • 34
  • 70
yuukiroh
  • 67
  • 2
  • 5

3 Answers3

0

You could override __getattr__ method

def __getattr__(self, item):
    values = ["hemisphere", "degrees", "minutes", "seconds"]
    if item in values:
        return self._correction()[values.index(item)]
    raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{item}'")
azro
  • 53,056
  • 7
  • 34
  • 70
0

There's a bunch of ways you could shorten this, I would write another helper function:

from functools import wraps

def unpack(rng, fn, propertize=False):
    for i in rng:
        @wraps(fn)
        def wrapper(*args, __i=i, **kwargs):
            return fn(*args, **kwargs)[i]
        yield property(wrapper) if propertize else wrapper

...

class Something:
    hemisphere, degrees, minutes, seconds = unpack(
        range(4), Something._correction, propertize=True
    )
L3viathan
  • 26,748
  • 2
  • 58
  • 81
0

As @xjcl commented, and on the basis of simplicity in preference to seemingly unecessary complication/obfuscation:

class....

    @property
    def degrees(self):
        return self._correction()[1]

    @property
    def minutes(self):
        return self._correction()[2]

    @property
    def seconds(self):
        return self._correction()[3]