A (unpythonic) pythonable mixin recipe.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paolino

    #1

    A (unpythonic) pythonable mixin recipe.

    I had always been negative on the boldeness of python on insisting that
    unbound methods should have been applied only to its im_class instances.
    Anyway this time I mixed in rightly, so I post this for comments.

    ###### looking for a discovery .Start ############### ##

    class _Mixin(object):
    def __init__(self,m ain,instance,*a rgs,**kwargs):
    # do mixin businnes
    main.__reinit__ (self,instance) # the caveated interface
    # probably missing __reinit__ in main
    # one could assume main.__init__ should do
    def mixinMethod(sel f):
    print 'mixinMethod on',repr(self)

    def Mixin(instance, *args,**kwargs) :
    klass=instance. __class__
    return type('Mix+%s'%k lass.__name__,( _Mixin,klass),{ })(klass,instan ce)

    ############ end of hot water discovery ##########

    class Base(object):
    def __reinit__(self ,another):
    # do something so that self is like another (painful in general)
    # easy for mutables, impossible for other
    pass

    b=Base()
    b=Mixin(b)

    assert isinstance(b,Ba se)
    b.mixinMethod() # doesn't fail with absurds

    #### The next doesn't work ####
    # l=[1,2,3]
    # l.__reinit__=l. __init__ # exception IMAConservative Language

    class L(list):
    __reinit__=lamb da self,other:list .__init__(self, other)

    l=L([1,2,3])
    l=Mixin(l)
    l.mixinMethod()

    Regards Paolino







    _______________ _______________ _____
    Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB

Working...