Wrapper objects
Nick Coghlan
ncoghlan at iinet.net.au
Fri Dec 10 23:59:39 EST 2004
redhog at takeit.se wrote:
> The wrapper objects needs to work as dictionary keys, to support
> printing, concatenation/addition, getitem/setitem and such things...
In that case, identifying exactly which operations you want to support, and
using a metaclass based approach like mine or Bengt's should work.
However, I highly recommend the 'opt-in' approach to delegating magic methods.
As Bengt discovered, there are a number of them that can break your class badly
if you inadvertently delegate them (e.g. if you delegate __new__ or __init__,
your class cannot be instantiated).
The Python Language Reference can tell you which methods need to be delegated to
support a given operation: http://docs.python.org/ref/specialnames.html
The delegation may still not be completely transparent, since the delegated
methods may not know how to deal with your wrapper objects. So you may require
calls to methods like int() or str() in your application code to make the types
work out correctly.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-list
mailing list