Tag Archives: Classes

Old style and new style classes in Python


New style classes inherit from object or from another new style class:

class BaseClass(object):

class DerivedClass(BaseClass)


A descriptor is an object attribute that has been defined in such a way that it can intercept its access and perform user-defined behaviour. Descriptor classes override methods called __get__, __set__ and __delete__, which are called when the attribute is read, written, or deleted respectively.

As an example, here is a descriptor class that enforces a read-only attribute:

class ReadOnly(object):
    def __init__(self, val):
        self.val = val
    def __get__(self, obj, type=None):
        return self.val
    def __set__(self, obj, val):
        raise AttributeError("Attempt to modify read-only attribute")

class MyClass(object):
    val = ReadOnly(10)

def main():
    m = MyClass()
    print m.val
        m.val = 9
    except AttributeError as err:
        print err

if __name__ == '__main__':

See: How-To Guide for Descriptors

Method Resolution Order (MRO)

This is now based on the C3 algorithm

See: The Python 2.3 Method Resolution Order