Automatically generating arithmetic operations for a subclass

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Apr 15 05:17:44 EDT 2009


On Tue, 14 Apr 2009 19:24:56 +0200, Sebastian Wiesner wrote:

>> Is there a trick or Pythonic idiom to make arithmetic operations on a
>> class return the same type, without having to manually specify each
>> method? I'm using Python 2.5, so anything related to ABCs are not an
>> option.
>> 
>> Does anyone have any suggestions?
> 
> Metaclasses can be used for this purpuse, see the example for a Roman
> number type [1]
> 
> [1] http://paste.pocoo.org/show/97258/


That's an interesting solution. I like it.


Thanks to all who responded, I see that there's no best practice to get 
what I want, so I'll do some experimentation.



-- 
Steven



More information about the Python-list mailing list