Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Partial 1.0 - Partial classes for Python

Reply
Thread Tools

Re: Partial 1.0 - Partial classes for Python

 
 
Thomas Heller
Guest
Posts: n/a
 
      02-07-2007
Martin v. Löwis schrieb:
> I'm happy to announce partial 1.0; a module to implement
> partial classes in Python. It is available from
>
> http://cheeseshop.python.org/pypi/partial/1.0
>
> A partial class is a fragment of a class definition;
> partial classes allow to spread the definition of
> a class over several modules. One location serves
> as the original definition of the class.
>
> To extend a class original_module.FullClass with
> an additional function, one writes
>
> from partial import *
> import original_module
>
> class ExtendedClass(partial, original_module.FullClass):
> def additional_method(self, args):
> body
> more_methods
>
> This module is licensed under the Academic Free License v3.0.
>
> Please send comments and feedback to


Nice idea. I had to apply this change to make it work, though:

diff -u c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py.orig c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py
--- c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py.orig Wed Feb 07 13:47:55 2007
+++ c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py Wed Feb 07 13:47:55 2007
@@ -36,7 +36,7 @@
continue
if k in base.__dict__ and not hasattr(v, '__replace'):
raise TypeError, "%s already has %s" % (repr(base), k)
- base.__dict__[k] = v
+ setattr(base, k, v)
# Return the original class
return base

otherwise I get this:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python25\lib\site-packages\partial-1.0-py2.5.egg\partial.py", line 39, in __new__
base.__dict__[k] = v
TypeError: Error when calling the metaclass bases
'dictproxy' object does not support item assignment


IIUC, wouldn't be 'partial.extend' or something like that be a better name
for the base class?

Thanks,
Thomas
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      02-07-2007
On Feb 7, 8:51 am, Thomas Heller <thel...@ctypes.org> wrote:
> Martin v. Löwis schrieb:
>
> > I'm happy to announce partial 1.0; a module to implement
> > partial classes in Python. It is available from

>
> >http://cheeseshop.python.org/pypi/partial/1.0

>
> > A partial class is a fragment of a class definition;
> > partial classes allow to spread the definition of
> > a class over several modules. One location serves
> > as the original definition of the class.

>
> > To extend a class original_module.FullClass with
> > an additional function, one writes

>
> > from partial import *
> > import original_module

>
> > class ExtendedClass(partial, original_module.FullClass):
> > def additional_method(self, args):
> > body
> > more_methods

>
> > This module is licensed under the Academic Free License v3.0.

>
> > Please send comments and feedback to mar...@v.loewis.de

>
> Nice idea.


Indeed. I was going to make a post asking for advice on high-level
delegation (basically you have a generic mostly-OO framework, which
the user extends mostly by subclassing, but how do the generic classes
know about the user-extened classes?). I knew of many solutions, but
all had significant drawbacks. But this seems like it'd work great,
maybe with a few minor inconveniences but nothing like the icky hacks
I've been using.

Ironic, since I myself posted a very simple example of how to do this
with a class hook here on c.l.python a while back.

But I'll have to review the license and see how it works with what I
have.


Carl Banks

 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      02-07-2007
On Feb 7, 10:17 am, "Carl Banks" <pavlovevide...@gmail.com> wrote:
> On Feb 7, 8:51 am, Thomas Heller <thel...@ctypes.org> wrote:
>
>
>
> > Martin v. Löwis schrieb:

>
> > > I'm happy to announce partial 1.0; a module to implement
> > > partial classes in Python. It is available from

>
> > >http://cheeseshop.python.org/pypi/partial/1.0

>
> > > A partial class is a fragment of a class definition;
> > > partial classes allow to spread the definition of
> > > a class over several modules. One location serves
> > > as the original definition of the class.

>
> > > To extend a class original_module.FullClass with
> > > an additional function, one writes

>
> > > from partial import *
> > > import original_module

>
> > > class ExtendedClass(partial, original_module.FullClass):
> > > def additional_method(self, args):
> > > body
> > > more_methods

>
> > > This module is licensed under the Academic Free License v3.0.

>
> > > Please send comments and feedback to mar...@v.loewis.de

>
> > Nice idea.

>
> Indeed. I was going to make a post asking for advice on high-level
> delegation (basically you have a generic mostly-OO framework, which
> the user extends mostly by subclassing, but how do the generic classes
> know about the user-extened classes?). I knew of many solutions, but
> all had significant drawbacks. But this seems like it'd work great,
> maybe with a few minor inconveniences but nothing like the icky hacks
> I've been using.
>
> Ironic, since I myself posted a very simple example of how to do this
> with a class hook here on c.l.python a while back.


And looking back at that post, I said that using such a hack would be
"truly evil". To every thing there is a season....


Carl Banks

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      02-07-2007
> Martin v. Löwis schrieb:
>
>
>
> > I'm happy to announce partial 1.0; a module to implement
> > partial classes in Python. It is available from

>
> >http://cheeseshop.python.org/pypi/partial/1.0

>
> > A partial class is a fragment of a class definition;
> > partial classes allow to spread the definition of
> > a class over several modules. One location serves
> > as the original definition of the class.


This looks the same as Ruby classes, right?
I don't like class definitions to be scattered in different files, for
the same reason
I don't like excessive inheritance, it is too easy to get lost when
debugging
code written by somebody else. But probably I am just overreacting due
to my
exposure to Zope ...

Michele Simionato

 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      02-07-2007
On Feb 7, 10:48 am, "Michele Simionato" <michele.simion...@gmail.com>
wrote:
> > Martin v. Löwis schrieb:

>
> > > I'm happy to announce partial 1.0; a module to implement
> > > partial classes in Python. It is available from

>
> > >http://cheeseshop.python.org/pypi/partial/1.0

>
> > > A partial class is a fragment of a class definition;
> > > partial classes allow to spread the definition of
> > > a class over several modules. One location serves
> > > as the original definition of the class.

>
> This looks the same as Ruby classes, right?
> I don't like class definitions to be scattered in different files, for
> the same reason
> I don't like excessive inheritance, it is too easy to get lost when
> debugging
> code written by somebody else. But probably I am just overreacting due
> to my
> exposure to Zope ...


The big difference from Ruby is that this can't extend most built-
ins. Which means that a lot of Ruby horror stories wouldn't apply.


Carl Banks

 
Reply With Quote
 
Thomas Heller
Guest
Posts: n/a
 
      02-07-2007
Carl Banks schrieb:
> On Feb 7, 10:17 am, "Carl Banks" <pavlovevide...@gmail.com> wrote:
>> On Feb 7, 8:51 am, Thomas Heller <thel...@ctypes.org> wrote:
>>
>>
>>
>> > Martin v. Löwis schrieb:

>>
>> > > I'm happy to announce partial 1.0; a module to implement
>> > > partial classes in Python. It is available from

>>
>> > >http://cheeseshop.python.org/pypi/partial/1.0

>>
>> > > A partial class is a fragment of a class definition;
>> > > partial classes allow to spread the definition of
>> > > a class over several modules. One location serves
>> > > as the original definition of the class.

>>
>> > > To extend a class original_module.FullClass with
>> > > an additional function, one writes

>>
>> > > from partial import *
>> > > import original_module

>>
>> > > class ExtendedClass(partial, original_module.FullClass):
>> > > def additional_method(self, args):
>> > > body
>> > > more_methods

>>
>> > > This module is licensed under the Academic Free License v3.0.

>>
>> > > Please send comments and feedback to mar...@v.loewis.de

>>
>> > Nice idea.

>>
>> Indeed. I was going to make a post asking for advice on high-level
>> delegation (basically you have a generic mostly-OO framework, which
>> the user extends mostly by subclassing, but how do the generic classes
>> know about the user-extened classes?). I knew of many solutions, but
>> all had significant drawbacks. But this seems like it'd work great,
>> maybe with a few minor inconveniences but nothing like the icky hacks
>> I've been using.
>>
>> Ironic, since I myself posted a very simple example of how to do this
>> with a class hook here on c.l.python a while back.

>
> And looking back at that post, I said that using such a hack would be
> "truly evil". To every thing there is a season....


Do you have a pointer to that post?

Thomas
 
Reply With Quote
 
Ziga Seilnacht
Guest
Posts: n/a
 
      02-07-2007
Thomas Heller wrote:
>
> Do you have a pointer to that post?
>


I think that he was refering to this post:
http://mail.python.org/pipermail/pyt...er/416241.html

If you are interested in various implementations there is also this:
http://mail.python.org/pipermail/pyt...st/396835.html

and a module from PyPy:
http://mail.python.org/pipermail/pyt...ly/067501.html

which was moved to a new location:
https://codespeak.net/viewvc/pypy/di...py?view=markup

Ziga


 
Reply With Quote
 
greg
Guest
Posts: n/a
 
      02-08-2007
> Martin v. Löwis schrieb:
>
>>A partial class is a fragment of a class definition;
>>partial classes allow to spread the definition of
>>a class over several modules.


When I want to do this, usually I define the parts
as ordinary, separate classes, and then define the
main class as inheriting from all of them. That
avoids the monkeypatching-like behaviour of what
you're doing -- the main class definition makes it
clear what parts it's made up of -- and it uses
only standard Python techniques, so it doesn't
harbour any surprises.

--
Greg
 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      02-08-2007

greg> When I want to do this, usually I define the parts as ordinary,
greg> separate classes, and then define the main class as inheriting
greg> from all of them.

Agreed. Maybe it's just my feeble brain, but I find this the most
compelling (and easy to understand) use for multiple inheritance by far.

Skip
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      02-08-2007
On Feb 8, 1:04 pm, s...@pobox.com wrote:
> greg> When I want to do this, usually I define the parts as ordinary,
> greg> separate classes, and then define the main class as inheriting
> greg> from all of them.
>
> Agreed. Maybe it's just my feeble brain, but I find this the most
> compelling (and easy to understand) use for multiple inheritance by far.
>
> Skip


That is a common design, but I don't like it, since it becomes very
easy to get
classes with dozens of methods inherited from everywhere, a modern
incarnation
of the spaghetti-code concept. I find it much better to use
composition, i.e. to
encapsulate the various behaviors in different objects and to add them
as
attributes. In other words, instead of a flat class namespace with
hundreds
of methods, I prefer a hierarchical namespace, a class with few
attributes, which
in turns have their own attributes, and so on. In other words, nested
is better
than flatten


Michele Simionato

P.S. Of course I mean situations where the methods can be meaningfully
grouped
together, which I find is the most common case.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Partial 1.0 - Partial classes for Python J. Clifford Dyer Python 0 02-08-2007 05:29 PM
Why? Partial Class within a Partial class Billy ASP .Net 2 02-01-2006 09:10 AM
Visual Studio 2005 (Beta 1) and auto-generated partial classes C.Batt ASP .Net 6 05-02-2005 03:48 PM
Is there a way to sign partial classes in Whidbey? =?Utf-8?B?VmFkaW0=?= ASP .Net 3 01-13-2005 12:11 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57