Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Partial classes

0 views
Skip to first unread message

Sanjay

unread,
Jul 19, 2006, 2:10:50 AM7/19/06
to
Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

GeneratedPerson.rb
Class Person
.
.
.

End Class

HandcraftedPerson.rb
Class Person
.
.
.
End Class

The intrepretor adds the code in both to compose the class Person.

What is the equivalent in Python? Inheriting is a way, but is not
working in all scenerios.

Thanks
Sanjay

alex23

unread,
Jul 19, 2006, 2:41:37 AM7/19/06
to
Sanjay wrote:
> Not being able to figure out how are partial classes coded in Python.

Hi Sanjay,

To the best of my knowledge, Python currently has no support for
partial classes.

However, BOO (http://boo.codehaus.org/) - which is a Python-like
language for the .NET CLI)- _does_ support partial classes
(http://jira.codehaus.org/browse/BOO-224). While it _isn't_ Python,
there could be enough similarities to make this worthwhile for you if
you absolutely have to have partial classes.

(Disclaimer: I've never used BOO)

Hope this helps.

-alex23

Kay Schluehr

unread,
Jul 19, 2006, 2:54:33 AM7/19/06
to

Python has no notion of a partial class because it is a pure compile
time construct. You might merge different class definitions by means of
a meta class that puts everything together but whether or not certain
methods in the merged class are available depends on which modules are
imported. This might give rise to a "virtual" or runtime module. It is
not a module that refers to a physical file on the disc but is a pure
runtime construct. When creating this module all physical modules that
define class fragments might be put together by means of the metaclass
mechanism. I indeed used this construction to unify different access
points before I reimplemented it using partial classes in C# which are
very fine IMO.

Sanjay

unread,
Jul 19, 2006, 3:00:10 AM7/19/06
to
Hi Alex,

Thanks for the input.

Being new to Python, and after having selected Python in comparison to
ruby (Turbogears vs Rails) , is jerks me a bit. In my openion it should
be an obvious and easy to implement feature and must be, if not already
have been, planned in future releases of Python.

Would love to listen to others.

Sanjay

Marc 'BlackJack' Rintsch

unread,
Jul 19, 2006, 3:21:40 AM7/19/06
to

> Being new to Python, and after having selected Python in comparison to
> ruby (Turbogears vs Rails) , is jerks me a bit. In my openion it should
> be an obvious and easy to implement feature and must be, if not already
> have been, planned in future releases of Python.

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?

Ciao,
Marc 'BlackJack' Rintsch

Dave Benjamin

unread,
Jul 19, 2006, 3:22:55 AM7/19/06
to
On Wed, 18 Jul 2006, Sanjay wrote:

> What is the equivalent in Python? Inheriting is a way, but is not
> working in all scenerios.

Have you tried multiple inheritance? For example:

from GeneratedPerson import GeneratedPerson
from HandcraftedPerson import HandcraftedPerson

class Person(GeneratedPerson, HandcraftedPerson):
pass

If this doesn't work for you, can you explain why?

Dave

Daniel Dittmar

unread,
Jul 19, 2006, 3:58:43 AM7/19/06
to

# HandcraftedPerson.py
import GeneratedPerson

class Person:
def somemethod (self):
pass


GeneratedPerson.Person.somemethod = Person.somemethod

Using reflection to merge all methods of HandcraftedPerson.Person into
GeneratedPerson.Person is left as an exercise.

Daniel

Peter Otten

unread,
Jul 19, 2006, 4:08:36 AM7/19/06
to
Sanjay wrote:

I, like everybody else it seems, am interested to know why/when (multiple)
inheritance doesn't work. Meanwhile

# this is a hack
import inspect
import textwrap

class Generated:
def generated(self):
print "generated"

def class_body(Class):
return textwrap.dedent(inspect.getsource(Class).split("\n", 1)[1])

class Handmade:
exec class_body(Generated)
def handmade(self):
print "handmade"

if __name__ == "__main__":
print dir(Handmade)
Handmade().generated()

Peter

Sanjay

unread,
Jul 19, 2006, 4:13:26 AM7/19/06
to
> 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.

Database script:

CREATE TABLE person (
id SERIAL,
passport VARCHAR(50) NOT NULL,
blocked BOOLEAN NOT NULL DEFAULT FALSE,
first_name VARCHAR(30) NOT NULL,
middle_name VARCHAR(30) NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(100) NOT NULL,
used_bytes INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(id)
);

CREATE TABLE contact (
person_id INTEGER NOT NULL REFERENCES person,
contact_id INTEGER NOT NULL REFERENCES person,
favorite BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY(person_id, contact_id)
);

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
contact_tbl = Table('contact', default_metadata, autoload = True)
class Contact(object):
pass
assign_mapper(Person, person_tbl, properties = {
'contacts' :
relation(Contact,
primaryjoin=person_tbl.c.id==contact_tbl.c.person_id,
association=Person)
})

assign_mapper(Contact, contact_tbl, properties = {
'person' :
relation(Person,
primaryjoin=person_tbl.c.id==contact_tbl.c.contact_id)
})

Business logic in another module, say bo.py

Class PersonBO(Person):
def Block():
blocked = True

While using PersonBO in another module, like this:

p1 = PersonBO(passport = "skpa...@hotmail.com", first_name='john',
last_name='smith', email = "skpa...@yahoo.com")
p2 = PersonBO(passport = "skpa...@yahoo.com", first_name='ed',
last_name='helms', email = "skpa...@yahoo.com")
p3 = PersonBO(passport = "skpa...@yahoo.com", first_name='jonathan',
last_name='lacour', email = "skpa...@yahoo.com")

# add a contact
p1.contacts.append(Contact(person=p2))

the following error message occurs:

AttributeError: 'PersonBO' object has no attribute 'contacts'

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.

The point in general, to my knowledge, about inheritance is that it
can't be a substitute for all the usages of partical classes. Metaclass
is a new concept for me, which I have to study.

As far as my project is concerned, I have found out some other way of
doing the things, and it is no more an issue.

Thanks a lot,
Sanjay

Bruno Desthuilliers

unread,
Jul 19, 2006, 5:08:19 AM7/19/06
to

I've never had a use case for this kind of feature in the past seven years.

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

Kay Schluehr

unread,
Jul 19, 2006, 5:54:53 AM7/19/06
to

Bruno Desthuilliers wrote:
> Sanjay wrote:
> > Hi Alex,
> >
> > Thanks for the input.
> >
> > Being new to Python, and after having selected Python in comparison to
> > ruby (Turbogears vs Rails) , is jerks me a bit. In my openion it should
> > be an obvious and easy to implement feature and must be, if not already
> > have been, planned in future releases of Python.
> >
> > Would love to listen to others.
>
> I've never had a use case for this kind of feature in the past seven years.

Interesting. Are there other use cases you did not have too?

bria...@gmail.com

unread,
Jul 19, 2006, 6:38:19 AM7/19/06
to
Sanjay wrote:
> Hi All,
>
> Not being able to figure out how are partial classes coded in Python.
>
> Example: Suppose I have a code generator which generates part of a
> business class, where as the custome part is to be written by me. In
> ruby (or C#), I divide the code into two source files. Like this:

I would do this by inheritance if really needed - what isn't working?
That said, you can get this behaviour fairly easy with a metaclass:

class PartialMeta(type):
registry = {}
def __new__(cls,name,bases,dct):
if name in PartialMeta.registry:
cls2=PartialMeta.registry[name]
for k,v in dct.items():
setattr(cls2, k, v)
else:
cls2 = type.__new__(cls,name,bases,dct)
PartialMeta.registry[name] = cls2
return cls2

class PartialClass(object):
__metaclass__=PartialMeta

use:
#generatedperson.py
class Person(PartialClass):
def foo(self): print "foo"

#gandcraftedperson.py
import generatedperson
class Person(PartialClass):
def bar(self): print "bar"

and you should get similar behaviour.

Caveats:
I've used just the name to determine the class involved to be the
same as your Ruby example. However, this means that any class in any
namespace with the name Person and inheriting from PartialClass will be
interpreted as the same - this might not be desirable if some other
library code has a different Person object doing the same thing. Its
easy to change to checking for some property instead - eg. have your
handcrafted class have the line "__extends__=generatedperson.Person" ,
and check for it in the metaclass instead of looking in a name
registry.

Also, if two or more classes define the same name, the last one
evaluated will overwrite the previous one.

Bruno Desthuilliers

unread,
Jul 19, 2006, 7:39:06 AM7/19/06
to
Probably quite a lot, why ?-)

Sanjay

unread,
Jul 19, 2006, 7:41:02 AM7/19/06
to
Thanks for the code showing how to implement partial classes. Infact, I
was searching for this code pattern. I will have a study on metaclass
and then try it.

Thanks
Sanjay

Kay Schluehr

unread,
Jul 19, 2006, 7:46:10 AM7/19/06
to

bria...@gmail.com wrote:
> Sanjay wrote:
> > Hi All,
> >
> > Not being able to figure out how are partial classes coded in Python.
> >
> > Example: Suppose I have a code generator which generates part of a
> > business class, where as the custome part is to be written by me. In
> > ruby (or C#), I divide the code into two source files. Like this:
>
> I would do this by inheritance if really needed - what isn't working?
> That said, you can get this behaviour fairly easy with a metaclass:
>
> class PartialMeta(type):
> registry = {}
> def __new__(cls,name,bases,dct):
> if name in PartialMeta.registry:
> cls2=PartialMeta.registry[name]
> for k,v in dct.items():
> setattr(cls2, k, v)
> else:
> cls2 = type.__new__(cls,name,bases,dct)
> PartialMeta.registry[name] = cls2
> return cls2
>
> class PartialClass(object):
> __metaclass__=PartialMeta

This definition lacks a check for disjointness of the parts. No two
partial classes shall contain a method with the same name.

bria...@gmail.com

unread,
Jul 19, 2006, 8:26:02 AM7/19/06
to

Kay Schluehr wrote:

> This definition lacks a check for disjointness of the parts. No two
> partial classes shall contain a method with the same name.

Yes - I mentioned at the bottom that the last one evaluated will
overwrite any existing one. You're right that its probably a better
idea to check for it and throw an exception.

John Salerno

unread,
Jul 19, 2006, 9:56:21 AM7/19/06
to
Marc 'BlackJack' Rintsch 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?

From my experience with C#, the only real use for partial classes is
when you want to separate your GUI code from the rest of your logic. But
since GUI programming in Python isn't always done, this isn't as big of
a deal.

Aside from that, if you really need to split up your classes, it's
probably an indication that you could create multiple classes instead,
right?

Bruno Desthuilliers

unread,
Jul 19, 2006, 10:25:04 AM7/19/06
to
John Salerno wrote:
> Marc 'BlackJack' Rintsch 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?
>
>
> From my experience with C#, the only real use for partial classes is
> when you want to separate your GUI code from the rest of your logic.

What the ... is GUI code doing in a domain object ???

Bruno Desthuilliers

unread,
Jul 19, 2006, 10:39:00 AM7/19/06
to
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 = "skpa...@hotmail.com", first_name='john',
> last_name='smith', email = "skpa...@yahoo.com")
> p2 = PersonBO(passport = "skpa...@yahoo.com", first_name='ed',
> last_name='helms', email = "skpa...@yahoo.com")
> p3 = PersonBO(passport = "skpa...@yahoo.com", first_name='jonathan',
> last_name='lacour', email = "skpa...@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 ?

Kay Schluehr

unread,
Jul 19, 2006, 10:39:20 AM7/19/06
to

John Salerno wrote:
> Marc 'BlackJack' Rintsch 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?
>
> From my experience with C#, the only real use for partial classes is
> when you want to separate your GUI code from the rest of your logic. But
> since GUI programming in Python isn't always done, this isn't as big of
> a deal.

Experiences have the tendency to differ.

What about letting your teammates editing certain data-structures in
different files ( physical modules ) but using them in a uniform way
and enable a single access point. If you have partial classes there is
no reason why your team has to share a large file where they have to
edit a single class but break the class into different parts and edit
the parts separately. No one has to care for including any module
because the CLR fits all partial classes together at compile time. I
find this quite amazing.

> Aside from that, if you really need to split up your classes, it's
> probably an indication that you could create multiple classes instead,
> right?

Just infrastructure overhead. I thought people are Pythonistas here and
not Java minds?

Sanjay

unread,
Jul 19, 2006, 11:56:28 AM7/19/06
to
> > Class PersonBO(Person):
> > def Block():
> > blocked = True
>
> <OT>
> shouldn't it be:
> class PersonBO(Person):
> def block(self):
> self.blocked = True
> </OT>

Yes, it should be as you mentioned. However, I had posted it to
elaborate the case. Actually, I tested using the following code:

class PersonBO(Person):
pass

> > As far as my project is concerned, I have found out some other way of
> > doing the things,
>
> Care to explain your solution ?

For the time being, I am not separating the plumbing and business
logic. When I need to, I shall come back to this post, study all the
ideas suggested, and jot down the pattern suitable to me. The code
pattern using metaclass looked interesting to me.

Thanks
Sanjay

John Salerno

unread,
Jul 19, 2006, 12:42:55 PM7/19/06
to
Bruno Desthuilliers wrote:
> John Salerno wrote:
>> Marc 'BlackJack' Rintsch 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?
>>
>> From my experience with C#, the only real use for partial classes is
>> when you want to separate your GUI code from the rest of your logic.
>
> What the ... is GUI code doing in a domain object ???
>
>
>

well, as far as visual studio goes, it puts the GUI code in a partial
class that you don't see, and then you do the rest of your coding in the
other part of the same class...i suppose you can change it however you
like, but that's the way it starts...

Rob Williscroft

unread,
Jul 19, 2006, 2:08:58 PM7/19/06
to
Bruno Desthuilliers wrote in news:44be40c1$0$21612$636a...@news.free.fr
in comp.lang.python:

> John Salerno wrote:
>> Marc 'BlackJack' Rintsch 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?
>>
>>
>> From my experience with C#, the only real use for partial classes is
>> when you want to separate your GUI code from the rest of your logic.
>
> What the ... is GUI code doing in a domain object ???

It doesn't (shouldn't) really work like that.

The partial classes are used by GUI designer tools (Visual Studio[1]
and SharpDevelop[2] for example) to seperate code that the tool generates
from code (event handlers etc) that the programmer writes.

IOW hopefully both parts of the class have GUI code in them.

[1] http://msdn.microsoft.com/vstudio/
[2] http://www.sharpdevelop.net/OpenSource/SD/

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Michele Simionato

unread,
Jul 20, 2006, 12:21:02 AM7/20/06
to

Sanjay ha scritto:

Anyway, I would suggest you NOT to use this code in production. Yes,
Python
can imitate Ruby, but using this kind of classes would confuse
everybody and
make your code extremely unpythonic. As always, consider changing your
mindset,
when you switch language. For you problem, you could just put your
client
methods in a file, and add them to your original class with setattr, if
you don't
want to use inheritance. It would be still better that magically
transform your
classes with a metaclass.

Michele Simionato

Sanjay

unread,
Jul 20, 2006, 1:23:15 AM7/20/06
to
> Anyway, I would suggest you NOT to use this code in production. Yes,
> Python
> can imitate Ruby, but using this kind of classes would confuse
> everybody and
> make your code extremely unpythonic. As always, consider changing your
> mindset,
> when you switch language. For you problem, you could just put your
> client
> methods in a file, and add them to your original class with setattr, if
> you don't
> want to use inheritance. It would be still better that magically
> transform your
> classes with a metaclass.

Thanks for the much needed guidence.

Sanjay

Stefan Behnel

unread,
Jul 20, 2006, 1:49:53 AM7/20/06
to
Kay Schluehr wrote:
> What about letting your teammates editing certain data-structures in
> different files ( physical modules ) but using them in a uniform way
> and enable a single access point. If you have partial classes there is
> no reason why your team has to share a large file where they have to
> edit a single class but break the class into different parts and edit
> the parts separately. No one has to care for including any module
> because the CLR fits all partial classes together at compile time.

I can't believe I always used version control systems for that use case if
it's that easily solved with partial classes.

Stefan

Bruno Desthuilliers

unread,
Jul 20, 2006, 4:42:36 AM7/20/06
to

Collaborative work is only one of the needs solved by VCS.

0 new messages