[Python-Dev] Mixins.

Sokolov Yura falcon at intercable.ru
Mon Sep 19 10:52:33 CEST 2005


Excuse my english.

Is there any pythonic way to make real mixin into a class?
Problem: Django model (and may be SQLObject).
I have a common peaces of model declaration. Sometimes they are in table 
definition together,
sometimes  table have just one of them. I cannot  use  inheritance, 
cause  Django doesn't handle it
in right way (like mixins).
Would it be usefull to define new magic class variable __mixins__ for a 
new style classes, which
will accept list of classes and will drop all members of them into 
definition of class before they went to metaclass.

Possible example (my wish in Django's Model):
class time_from_to:
    time_from=meta.DateTimeField(default=datetime(1900,1,1,0,0,0))
    time_to=meta.DateTimeField(default=datetime(3000,1,1,0,0,0))

class ident(meta.Model):
    __mixin__=[time_from_to]
    ident = meta.TextField()

Or, as alternative:
class time_from_to(mixin):
    time_from=meta.DateTimeField(default=datetime(1900,1,1,0,0,0))
    time_to=meta.DateTimeField(default=datetime(3000,1,1,0,0,0))

class ident(time_from_to,meta.Model):
    ident = meta.TextField()

You say: "It is problem of Django". Yes, you are right.
It is common to use multiple inheritace in place of mixin. But it is 
handled not all time.
Django does not at the moment (I hope, it would).
To handle it, library writers, who implements their metaclasses, ought 
to write similar code again and again.
I think, it would be simpler to make mixins in one true standart way. I 
can be mistaken.




More information about the Python-Dev mailing list