CONSTRUCT - Adding Functionality to the Overall System
Ilias Lazaridis
ilias at lazaridis.com
Wed Sep 20 13:48:07 EDT 2006
George Sakkis wrote:
> Ilias Lazaridis wrote:
>
> > I like to add a method "writeDebug(self, msg)" to all (or the most
> > possible) classes in the system.
> >
> > How do I do this?
> >
> > * with new style classes
> > * with old style classes
>
> Short answer: you can't do it for builtin or extension types:
> >>> list.writeDebug = lambda msg: "You'd wish"
> ...
> TypeError: can't set attributes of built-in/extension type 'list'
>
> Longer asnwer: Make it a function instead of a method. This function
> could try to call the respective method, and as a fallback it would
> have hardcoded what to do for each supported class, something like:
>
> def writeDebug(obj, msg):
> try: return obj.writeDebug(msg)
> except AttributeError:
> if isinstance(obj,list):
> # list msg
> elif isinstance(obj,tuple):
> # tuple msg
> ...
> else:
> # default object msg
>
> If you insist though that you'd rather not use functions but only
> methods, tough luck; you're better off with Ruby.
I insist on methods, and It seems that I stay with Python.
The effort for me to rework python to become more OO is much lesser,
than the effort I would have to rework Ruby to become more (this and
that).
http://case.lazaridis.com/wiki/Lang
And I've already started to like 2 things on python:
* the missing "end" statement and
* (I don't believe I write this) enforced indentation.
.
More information about the Python-list
mailing list