[Python-Dev] method decorators (PEP 318) (original) (raw)

Josiah Carlson jcarlson at uci.edu
Sun Mar 28 23:56:03 EST 2004


> > and wouldn't bother with staticmethods at all. How often do you > > actually need a staticmethod in particular and not a classmethod? > > It's quite useful to be able to develop, essentially stateless, static > method utility libraries that dont need to be class based. Why create

I'm not sure I understand. Why not make them module-level functions?

Namespaces my friend, namespaces (I don't know if other people use this, but I have on occasion).

#example.py class functions_a: def foo(inp1, inp2) [staticmethod]: #do something #more functions that do something

class functions_b: def foo(inp1, inp2) [staticmethod]: #do something slightly different #more functions that do something different

#end example.py

We can use the classes as mini namespaces, and only need to import and distribute a single module. With class decorators (which seem to be in favor right now), we can go even a step farther with the following (which is another reason why class decorators are a good idea)...

def make_all_static(cls): for i,j in cls.dict.iteritems(): if isinstance(j, instancemethod) cls.dict[i] = staticmethod(j)

class functions_c [make_all_static]: def foo(inp1, inp2): #do something a bit more different #more functions that do something a bit more different

Once again, you can use classes as namespaces in a single module.



More information about the Python-Dev mailing list