Simple Customization : to string « Class « Python Tutorial






class MyRound(object):
    def __init__(self, val):
        assert isinstance(val, float), "Value must be a float!"
        self.value = round(val, 2)

rfm = MyRound(42)
print rfm
rfm = MyRound(4.2)
print rfm

class MyRound(object):
    def __init__(self, val):
        assert isinstance(val, float), "Value must be a float!"
        self.value = round(val, 2)

    def __str__(self):
        return str(self.value)

rfm = MyRound(5.590464)
print rfm
rfm = MyRound(5.5964)
print rfm

class MyRound(object):
    def __init__(self, val):
        assert isinstance(val, float), "Value must be a float!"
        self.value = round(val, 2)
    def __str__(self):
        return '%.2f' % self.value

rfm = MyRound(5.5964)
print rfm








11.31.to string
11.31.1.Simple Customization
11.31.2.Basic Customization
11.31.3.Numeric Customization
11.31.4.Representation of phone number in USA format: (xxx) xxx-xxxx.