A (unpythonic) pythonable mixin recipe.

Discussion in 'Python' started by Paolino, Aug 16, 2005.

  1. Paolino

    Paolino Guest

    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,main,instance,*args,**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(self):
    print 'mixinMethod on',repr(self)

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

    ############ 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,Base)
    b.mixinMethod() # doesn't fail with absurds

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

    class L(list):
    __reinit__=lambda 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
    http://mail.yahoo.it
    Paolino, Aug 16, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Abrahams

    Unpythonic Python

    David Abrahams, Aug 24, 2004, in forum: Python
    Replies:
    7
    Views:
    387
    David Abrahams
    Aug 25, 2004
  2. Sockets ()

    , Apr 11, 2005, in forum: Python
    Replies:
    0
    Views:
    271
  3. Peter Corbett

    A rather unpythonic way of doing things

    Peter Corbett, Sep 29, 2005, in forum: Python
    Replies:
    13
    Views:
    459
    Tom Anderson
    Oct 2, 2005
  4. BrJohan

    Unpythonic? Impossible??

    BrJohan, Mar 19, 2006, in forum: Python
    Replies:
    7
    Views:
    269
    Erik Max Francis
    Mar 19, 2006
  5. J Kenneth King

    unpythonic use of property()?

    J Kenneth King, Apr 17, 2009, in forum: Python
    Replies:
    8
    Views:
    348
    J Kenneth King
    Apr 22, 2009
Loading...

Share This Page