[Python-Dev] Class decorators

Raymond Hettinger raymond.hettinger at verizon.net
Thu Mar 30 12:56:23 CEST 2006


[Jack Diederich]
>> Classes have a unique property in that they are the easiest way to make
>> little namespaces in python.

[Greg Ewing]
> For a while now, I've been wondering whether it would
> be worth having a construct purely for creating little
> namespaces, instead of abusing a class for this.

FWIW, I do not consider it an abuse to use a class to create a small namespace. 
Essentially that is what it is for -- it matters not whether the class has no 
methods.

What I've been wanting is the ability to easily define functions directly into 
an existing namespace:

    class A:
        pass

    def A.mymethod(x):
        return x+1

The latter definition is equivalent to:

    A.mymethod = lambda(x): x+1

This ability to inject function definitions into an existing namespace is the 
one missing piece to enable clean use of Protocol OO techniques:

    a = Object.copy()
    b = a.copy()
    def b.f(x):
        return x+1
    c = b.copy()
    def c.f(x):
        return x+2


Raymond






More information about the Python-Dev mailing list