Is there any way I can shorten properties with similar statements but different returns?

94 Views Asked by At
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.

3

There are 3 best solutions below

1
On BEST ANSWER

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]
3
On

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}'")
1
On

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
    )