By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,763 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,763 IT Pros & Developers. It's quick & easy.

Partial classes

P: n/a
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
Jul 19 '06 #1
Share this Question
Share on Google+
26 Replies


P: n/a
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

Jul 19 '06 #2

P: n/a

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:

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
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.

Jul 19 '06 #3

P: n/a
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

Jul 19 '06 #4

P: n/a
In <11**********************@i3g2000cwc.googlegroups. com>, Sanjay wrote:
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
Jul 19 '06 #5

P: n/a
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
Jul 19 '06 #6

P: n/a
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:

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.
# 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
Jul 19 '06 #7

P: n/a
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:

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.
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
Jul 19 '06 #8

P: n/a
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 = "sk*******@hotmail.com", first_name='john',
last_name='smith', email = "sk*******@yahoo.com")
p2 = PersonBO(passport = "sk*******@yahoo.com", first_name='ed',
last_name='helms', email = "sk*******@yahoo.com")
p3 = PersonBO(passport = "sk*******@yahoo.com", first_name='jonathan',
last_name='lacour', email = "sk*******@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

Jul 19 '06 #9

P: n/a
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #10

P: n/a

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?

Jul 19 '06 #11

P: n/a
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.

Jul 19 '06 #12

P: n/a
Kay Schluehr wrote:
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?
Probably quite a lot, why ?-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #13

P: n/a
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

Jul 19 '06 #14

P: n/a

br******@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.

Jul 19 '06 #15

P: n/a

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.

Jul 19 '06 #16

P: n/a
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?
Jul 19 '06 #17

P: n/a
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
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #18

P: n/a
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 = "sk*******@hotmail.com", first_name='john',
last_name='smith', email = "sk*******@yahoo.com")
p2 = PersonBO(passport = "sk*******@yahoo.com", first_name='ed',
last_name='helms', email = "sk*******@yahoo.com")
p3 = PersonBO(passport = "sk*******@yahoo.com", first_name='jonathan',
last_name='lacour', email = "sk*******@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 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #19

P: n/a

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?

Jul 19 '06 #20

P: n/a
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

Jul 19 '06 #21

P: n/a
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...
Jul 19 '06 #22

P: n/a
Bruno Desthuilliers wrote in news:44***********************@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/
Jul 19 '06 #23

P: n/a

Sanjay ha scritto:
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
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

Jul 20 '06 #24

P: n/a
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

Jul 20 '06 #25

P: n/a
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
Jul 20 '06 #26

P: n/a
Stefan Behnel wrote:
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.
Collaborative work is only one of the needs solved by VCS.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 20 '06 #27

This discussion thread is closed

Replies have been disabled for this discussion.