8 2592
On Thu, 06 Oct 2005 21:03:33 -0700, barnesc wrote: I added some recipes to the Python Cookbook:
- listmixin
Use ListMixin to create custom list classes from a small subset of list methods:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440656
That looks great. Now, if only I understood mixins: what are they, and
what they are for, and in particular, how they differ from mere
subclassing.
I've spent some time searching for a good definition on the web, but the
main problem I've found is that most discussions on mixins assume
you already know what they are. Those that don't are talking about
Ruby or Lisp or some other language other than Python. I don't speak
those languages, so their code examples don't make a lot of sense to me.
Anyhoo, I've struggled on, but obviously I Just Don't Get It because
using mixins looks like subclassing to me.
Can anyone help me understand what's going on?
Thanks,
--
Steven.
Well, suppose you have a class MyObject and you want to add to it some
methods to make its instances into a database. You could put these
methods into another class called Storable (the mixin class).
Then you can mix MyObject with Storable and get what you want,
a class StorableObject inheriting both from Storable and MyObject.
Of course you can reuse Storable to make storable even other
classes, for instance you could define a StorableOtherOb ject
inheriting from OtherObject and Storable.
Once in a time, I thought mixins where a good idea; now I don't think
so since they are too easily abused (see Zope 2) and as a consequence
you get spaghetti-inheritance, where you have objects with methods
inherited from everywhere. So be very careful if you want to use
mixins; often you can go without them.
Just my 2 Euro cents,
Michele Simionato
Steven D'Aprano <st***@REMOVETH IScyber.com.au> writes: That looks great. Now, if only I understood mixins: what are they, and what they are for, and in particular, how they differ from mere subclassing.
I'm not sure what you mean by "mere subclassing" so maybe there is no
difference. Mixins are sort of a design pattern, not any whiz-tech
thing. The terminology comes from Flavors which was one of the early
multiple inheritance OOP systems. Flavors terminology was inspired by
ice cream: you could go to Tosci's and get plain ice cream, or ice
cream with your choice of various mix-ins like raisins, M&M's, heath
bar pieces, etc., which you could combine orthogonally. In Flavors
and in Python, mix-ins are superclasses that you inherit to turn on a
feature. The classic example is a Window class, with mixins for
scroll bars, title bars, pulldown menu, etc. So if you want a window
with a scroll bar, you'd say
class Window_with_scr oll_bar(Window, Scrollbar): pass
and you'd get a class that understands the operations of both windows
and scroll bars. Flavors had automatic method combination that you
have to spell out manually in Python, making the scheme a little bit
less useful. But see SocketServer for a clever example of mixins to
implement concurrency in a TCP listener. The basic server class is
TCPServer and you inherit from ThreadingMixin to make a threading
server or ForkingMixin to make a forking server.
I guess what makes something a mix-in is that you normally don't
inherit from it directly. You inherit from some other class and the
mix-in is an "add-on" superclass that supplies some extra functionality.
"Michele Simionato" <mi************ ***@gmail.com> writes: Once in a time, I thought mixins where a good idea; now I don't think so since they are too easily abused (see Zope 2) and as a consequence you get spaghetti-inheritance, where you have objects with methods inherited from everywhere. So be very careful if you want to use mixins; often you can go without them.
Yeah, I wonder though how much of that is a result of Python's
cavalier approach to multiple inheritance. Does that happen much in
CLOS? In Java because of multiple interfaces? I've studied Flavors a
little and mix-ins were used in some extensive ways, but maybe
programs using them required extra care.
The Python tutorial does caution against indiscriminate use of
multiple inheritance. I tried coding something without it, wished
that I'd used it and did so in the next version, but still am not sure
if I gained anything or not.
On Fri, 07 Oct 2005 05:11:00 -0700, Michele Simionato wrote: Well, suppose you have a class MyObject and you want to add to it some methods to make its instances into a database. You could put these methods into another class called Storable (the mixin class). Then you can mix MyObject with Storable and get what you want, a class StorableObject inheriting both from Storable and MyObject. Of course you can reuse Storable to make storable even other classes, for instance you could define a StorableOtherOb ject inheriting from OtherObject and Storable.
So mixins are just a sub-class [pun intended] of sub-classing?
I've just found this:
[quote]
A mixin class is a parent class that is inherited from - but not as
a means of specialization. Typically, the mixin will export services to a
child class, but no semantics will be implied about the child "being a
kind of" the parent.
[end quote]
from http://c2.com/cgi/wiki?MixIn
Is that all they are?
It is amazing how you can take the simplest concept, and by using
appropriate terminology, make it as confusing and opaque as you want...
*wink*
--
Steven.
Steven D'Aprano <st***@REMOVETH IScyber.com.au> writes: On Thu, 06 Oct 2005 21:03:33 -0700, barnesc wrote: I added some recipes to the Python Cookbook:
- listmixin
Use ListMixin to create custom list classes from a small subset of list methods:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440656
That looks great. Now, if only I understood mixins: what are they, and what they are for, and in particular, how they differ from mere subclassing.
One way to look at it is that mixins are a special case of
subclassing. They aren't different, just specific.
Mixins are abstract. Using Paul's example, you'd never declare a
ScrollBar object, only subclasses that also inherit from some form of
Window.
Mixins are about behavior, not type. If you have real mixins, then an
object of a class that inherits from a mixin is not an instance of the
mixin class.
Mixins are one way to deal with the lack of multiple inheritance. But
even languages with multiple inheritance find the idea useful.
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Paul Rubin wrote: Yeah, I wonder though how much of that is a result of Python's cavalier approach to multiple inheritance. Does that happen much in CLOS? In Java because of multiple interfaces? I've studied Flavors a little and mix-ins were used in some extensive ways, but maybe programs using them required extra care.
I don't think Python approach to multiple inheritance is cavalier (you
may want
to clarify that statement). In CLOS multiple inheritance is less of a
problem
since (multi)methods are defined outside classes. One could argue that
using classes also as a scope-control mechanism (i.e. doing the job of
modules) is an abuse.
The Python tutorial does caution against indiscriminate use of multiple inheritance. I tried coding something without it, wished that I'd used it and did so in the next version, but still am not sure if I gained anything or not.
Nowadays I tend to use delegation via __getattr__ instead of multiple
inheritance.
Michele Simionato
"Steven D'Aprano" <st***@REMOVETH IScyber.com.au> wrote: I've just found this:
[quote] A mixin class is a parent class that is inherited from - but not as a means of specialization. Typically, the mixin will export services to a child class, but no semantics will be implied about the child "being a kind of" the parent. [end quote]
from http://c2.com/cgi/wiki?MixIn
Is that all they are?
It is amazing how you can take the simplest concept, and by using appropriate terminology, make it as confusing and opaque as you want...
*wink*
Now this reminds me of Xah's entertaining posts about "moroniciti es of the tech-geek industry
jargon" <wink>.
George This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Barry A. Warsaw |
last post by:
On behalf of the Python development team and the Python community, I'm
happy to announce the release of Python 2.3 (final).
Nineteen months in the making, Python 2.3 represents a commitment to
stability and improved performance, with a minimum of new language
features. Countless bugs and memory leaks have been fixed, many new
and updated modules have been added, and the new type/class system
introduced in Python 2.2 has been...
|
by: 2mc |
last post by:
Generally speaking, if one had a list (from regular Python) and an
array (from Numerical Python) that contained the same number of
elements, would a While loop or a For loop process them at the same
speed? Or, would the array process faster?
I'm new to Python, so my question may expose my ignorance. I
appreciate anyone's effort to help me understand.
Thanks. It is much appreciated.
|
by: Guyon Morée |
last post by:
Hi all,
I am working on a Huffman encoding exercise, but it is kinda slow. This is
not a big problem, I do this to educate myself :)
So I started profiling the code and the slowdown was actually taking place
at places where I didn't expect it.
after I have created a lookup-table-dictionary with encodings like
{'d':'0110', 'e':'01' etc} to encode the original text like this:
|
by: Simon Brunning |
last post by:
QOTW: "Sure, but what about the case where his program is on paper tape and
all he has for an editor is an ice pick?" - Grant Edwards
"And in this case, you get improved usability *and* improved speed at the
same time. That's the way it should be." - Fredrik Lundh
The Simplest Possible Metaclass:
http://orbtech.com/blog/simplemetaclass
|
by: Steven Bethard |
last post by:
George Sakkis wrote:
> "Steven Bethard" <steven.bethard@gmail.com> wrote:
>> Dict comprehensions were recently rejected:
>> http://www.python.org/peps/pep-0274.html
>> The reason, of course, is that dict comprehensions don't gain you
>> much at all over the dict() constructor plus a generator expression,
>> e.g.:
>> dict((i, chr(65+i)) for i in range(4))
>
> Sure, but the same holds for list comprehensions: list(i*i for i in
| |
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 mixin will export services to a
>child class, but no semantics will be implied about the child "being a
>kind of" the parent.
>
|
by: robert |
last post by:
Turning algs for old NumPy modules into numpy code I suffer from this:
Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy types.
First there is some inefficiency in calculations. And then you get data inflation and questionable dependencies - e.g. with pickle,ZODB,mpi's ... :
0.0...
|
by: Gabriel Genellina |
last post by:
QOTW: "here's always no best." - Lawrence Oluyede
http://groups.google.com/group/comp.lang.python/msg/32bce47d185 ce42e
"I actually do a lot of unit testing. I find it both annoying and highly
necessary and useful." - Steven Bethard
http://groups.google.com/group/comp.lang.python/msg/4df60bdff72540cb
Some confusion when using `if x:` as an existence test:...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |