An alternative approach to bound methods

Tim Couper tim at 2wave.net
Wed Feb 21 12:17:03 EST 2001


Jeremy Hylton wrote
> .... I'm not sure I really followed this argument.  Perhaps some code
> examples would help....
>

What attracted me to Marcin Kowalczy's original posting was that it appeared
to give a way of defining class functions, by merely extending the search
algorithm ("..obj.x, when obj is an instance object, first searches for 'x'
in its __dict__. If not found, it should try
obj.__class__.x ...")

Here's an example of the code I'd like to be able to run, extending a class
attributes example I was using:

class myclass:
    alist=['initial']

    def __init__(self,thelist=['the list']):
        self.__class__.alist=thelist

    def show(self):
        print self.__class__.alist

    """

    # I want to be able to declare a class function: (see unit test element
below)
    # so, as it's a class function, it doesn't need the self argument,
    # but is accessible through __class__. So I think it should take the
form
    #

    def static(args):
        #do something (only with class attributes)
        return SomeValue

    # or perhaps something like (but I think this is indistinguishable from
    # instance methods)

    def static(self,args):
        if not self:
           #do something (only with class attributes)
        else:
           # this is an instance call of this function
        return SomeValue
    """

if __name__=='__main__':

    print
    print 'Usual test'
    print '----------'
    print
    print 'string class attribute: ',myclass.astring

    a=myclass()
    print
    a.show()
    print

    b=myclass(['orig list'])
    a.show()
    b.show()

    print
    print 'Re-Assignment'
    print '-------------'

    a.__class__.alist=['new','list']

    a.show()
    b.show()

    print
    print 'Method'
    print '------'
    print

    # assuming that the static function returns something,
    # I'd like to write, in a completely analogous manner to the
    # class attribute:
    #
    # print myclass.static(args)
    # print a.__class__.static(args)
    # print b.__class__.static(args)







More information about the Python-list mailing list