Skip to content

Support __format__ #115

Description

@perhyps

Hi, I think it would be very helpful to support the formatting of measures by adding the following definition to the MeasureBase class:

def __format__(self, format_spec):
    return '%s %s' % (
        format(getattr(self, self._default_unit), format_spec),
        self.unit
    )

This would allow the user to write something like this (e.g. in Python 3.6+):

>>> from measurement.measures import Distance
>>> x = Distance(cm=5/7)
>>> print(x)
0.7142857142857143 cm
>>> print(f"{x:.3f}")
0.714 cm

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions