Pythonic way to add method alias in subclass

Lee Harr missive at frontiernet.net
Sat Dec 15 08:03:33 EST 2007


I thought of several ways to add another name for
a method in a subclass ...


#alias.py
class Foo(object):
    def nod(self):
        print "nodding"

class Bar(Foo):
    def __init__(self):
        self.agree = self.nod

class Bar2(Foo):
    agree = Foo.nod

class Bar3(Foo):
    def agree(self):
        Foo.nod(self)

def alias(method):
    def dec(m):
        return method
    return dec

class Bar4(Foo):
    @alias(Foo.nod)
    def agree(self):
        pass


b = Bar()
b.agree()

b2 = Bar2()
b2.agree()

b3 = Bar3()
b3.agree()

b4 = Bar4()
b4.agree()
#####################


I am leaning towards Bar2 since it has the least code.

Any thoughts?




More information about the Python-list mailing list