471,071 Members | 3,803 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

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,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),{})(kl ass,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
Aug 16 '05 #1
0 1233

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by zimba | last post: by
5 posts views Thread by Udo Gleich | last post: by
reply views Thread by barnesc | last post: by
6 posts views Thread by Alex Hunsley | last post: by
3 posts views Thread by Ed Leafe | last post: by
7 posts views Thread by BrJohan | last post: by
2 posts views Thread by ish | last post: by
1 post views Thread by Ole Nielsby | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.