The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by peter dann]

Great book. I've had it ten days, and have already written a useful utility
for myself.

Would appreciate some insight into the concept of a 'mix in' class. In the
chapter on OOP we learn that it's good practice to call the constructor of any
base classes before further constructing an object. Yet this isn't what occurs
for class JobListing(Persistent) on page 343.

We are told on page 336 that Persistent is a 'mix in' class. Does this mean it
is a class which has functions, but no data members that we need to
initialise? If so, is this all that is different about a 'mix in' class? Is
the only way to find out if a class is a mix in class to read its source code
and see if it declares any public data members?
import-bot (20211) [Avatar] Offline
#2
Re: mix in classes
[Originally posted by daryl harms]

> Great book. I've had it ten days, and have already written a useful utility
> for myself.
>
Thanks for this comment and I'm glad it has been useful.

> Would appreciate some insight into the concept of a 'mix in' class. In the
> chapter on OOP we learn that it's good practice to call the constructor of any
> base classes before further constructing an object. Yet this isn't what occurs
> for class JobListing(Persistent) on page 343.
>
Sounds like you've done some thorough reading in those ten days smilie.

The issue is that if you do not explicitly call the contructor it will not get
called. For example if you define and instantiate the following simple
classes:

>>> class Super:
def __init__(self):
print "in __init__ of Super"

>>> class A(Super):
def __init__(self):
print "in __init__ of A"

>>> class B(Super):
def __init__(self):
print "in __init__ of B"
Super.__init__(self)

>>> a = A()
in __init__ of A
>>> b = B()
in __init__ of B
in __init__ of Super
>>>


For class "A" the superclass's initializer is not called (which is a problem
if it does anything that needs to get done).

It is, however, possible to define a class with no initializer, which is the
case for the "Persistent" class used to add persistence to the class on page
343 in Amos and Brian's example.

However, from the rest of your note below, I figure you already have a good
understanding of this. So, I better get on to your real question.

> We are told on page 336 that Persistent is a 'mix in' class. Does this mean it
> is a class which has functions, but no data members that we need to
> initialise? If so, is this all that is different about a 'mix in' class? Is
> the only way to find out if a class is a mix in class to read its source code
> and see if it declares any public data members?

In this case you are right that "Persistent" has functions but no data members
that need to be initialized. However, this isn't a constraint of if being a
mix-in class. It would certainly be possible to create a mix-in class that
has an initializer that needs to be called.

A mix-in (or mix in or mixin) class is a term used for a class that is used to
"mix in" some functionality to other class.

I won't try to expand on this any further myself right now but will instead
refer you to three links where you can find already well thought out
explanations and examples of mixin classes:

http://www.devx.com/free/mgznarch/vcdj/1998/augmag98/mixin1.asp
http://www.kirkrader.com/examples/cpp/mixin.htm
http://www.borland.com/borlandcpp/news/cobb/bcj3_1d.html

In the last of these, Tim Gooch starts by discussing add-in classes. I've
often seen people use the term mix-in class more generally to refer to what I
would probably refer to as an add-in class.

Hope this helps,

Daryl