Search code examples
pythonclass-properties

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


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.


Solution

  • 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]