Partial classes

Bruno Desthuilliers onurb at xiludom.gro
Wed Jul 19 10:39:00 EDT 2006


Sanjay wrote:
>>Can you flesh out your use case a little bit and tell why you can't solve
>>the problem with inheritance or a meta class?
> 
> 
> I have to study about metaclass and see whether this can be handled. It
> seemed inheritence is not working.
> 
> PROBLEM: Separating plumbing code and business logic while using
> SQLAlchemy ORM.
> 
(snip)
> DB definitions and plumbing code goes into one module, say db.py:
> 
> import sqlalchemy.mods.threadlocal
> from sqlalchemy import *
> 
> global_connect('postgres://userid:password@localhost:5432/tm')
> person_tbl = Table('person', default_metadata, autoload = True)
> class Person(object):
>     pass
(snip)

> Business logic in another module, say bo.py
> 
> Class PersonBO(Person):
>      def Block():
>           blocked = True

<OT>
shouldn't it be:
class PersonBO(Person):
  def block(self):
    self.blocked = True
</OT>

> While using PersonBO in another module, like this:
> 
> p1 = PersonBO(passport = "skpatel20 at hotmail.com", first_name='john',
> last_name='smith', email = "skpatel20 at yahoo.com")
> p2 = PersonBO(passport = "skpatel20 at yahoo.com", first_name='ed',
> last_name='helms', email = "skpatel20 at yahoo.com")
> p3 = PersonBO(passport = "skpatel20 at yahoo.com", first_name='jonathan',
> last_name='lacour', email = "skpatel20 at yahoo.com")
> 
> # add a contact
> p1.contacts.append(Contact(person=p2))
> 
> the following error message occurs:
> 
> AttributeError: 'PersonBO' object has no attribute 'contacts'

Strange.

> What I guess, from my limited knowledge of the technologies involved,
> is that assign_mapper does some magic only on Person class, and things
> work. But after inheritence, it is not working.

Anyway, there are other ways to reuse implementation, like
composition/delegation. You may want to have a look at
__getattr__/__setattr__ magic methods.

> As far as my project is concerned, I have found out some other way of
> doing the things,

Care to explain your solution ?


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list