473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1317

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: zimba | last post by:
Hello ! If somebody is interested, here is a small hack I've done today. There are still some curious effects, but I'm pretty satisfied by the results, since PHP is not very flexible. Let...
5
by: Udo Gleich | last post by:
Hi, I try to implement mixin classes. Thats why I need to make a new class at runtime. --tmp.py------------------------------------- import new class K1(object):
1
by: Mac | last post by:
I have a MixIn class which defines a method foo(), and is then mixed in with another class by being prepended to that class's __bases__ member, thus overriding that class's definition of foo(). In...
0
by: barnesc | last post by:
>So mixins are just a sub-class of sub-classing? > >I've just found this: > > >A mixin class is a parent class that is inherited from - but not as >a means of specialization. Typically, the...
6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
3
by: Ed Leafe | last post by:
In Dabo, we create cursor classes that combine the backend-specific dbapi cursor class with our own mixin class that adds framework- specific behaviors. This has been working well for a couple of...
7
by: BrJohan | last post by:
Assume having this class hierarchy: (in principle and without details) class A(object): class B1(A): class B2(A): class C1(A1): class C2(A1): class C3(B1): class C4(B2): each of those...
2
by: ish | last post by:
I think this is more of a style question than anything else... I'm doing a C++ wrapper around a C event library I have and one of the items is a timer class, I'm also using this task to learn C++....
1
by: Ole Nielsby | last post by:
Given these 3 classes class A {virtual void a(){}}; class B {virtual void b(){}}; class C: public A, public B {}; I want the offset of B in C, as a size_t value, and preferably as a constant...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.