Writing good Classes in Python

December 2013 · 2 minute read

This post is the highlight of Raymond Hettinger’s talk “Python’s Class Development Toolkit“ at PyCon US 2013.

Things to keep in mind while writing Classes:

This is an example with all the above practices implemented:

import math

class GoodCircle(object):
    '''This represents the circle class'''

    __slots__ = ['diameter']        # Last Resort for memory efficiency
    version = '0.1'

    def __init__(self, radius):
        self.radius = radius

    @classmethod
    def from_bbd(cls, bbd):
        'Construct a Circle from bounding box diagonal'
        radius = bbd / 2.0 / math.sqrt(2.0)
        return cls(radius)

    @property
    def radius(self):
        return self.diameter / 2.0

    @radius.setter
    def radius(self, radius):
        self.diameter = radius * 2.0

    @staticmethod
    def angle_to_grade(angle):
        'Convert angle in degree to percentage degree'
        return math.tan(math.radians(angle)) * 100.0

    def area(self):
        p = self.__perimeter()
        r = p / math.pi / 2.0
        return math.pi * r ** 2.0

    def perimeter(self):
        return 2.0 * math.pi * self.radius

    __perimeter = perimeter

 

comments powered by Disqus