2009/1/29 Oleg Broytmann <p...@phd.pp.ru>:
> On Thu, Jan 29, 2009 at 08:06:18AM -0800, Aahz wrote:
>> On Thu, Jan 29, 2009, Michael Foord wrote:
>> > Don't we have a pretty-print API - and isn't it spelled __str__ ?
>>
>> In theory, yes.  In practice, we wouldn't be having this discussion if
>> that really worked.  But it probably would make sense to see how far
>> using __str__ can take us -- AFAICT enumobject.c doesn't define __str__
>> (although I may be missing something, I don't know Python at the C level
>> very well).
>
>   Container objects (tuples/lists/dicts/sets) don't define __str__.
> Is __pprint__ an attempt to redefine __str__?

Anyone feel like raising the topic of generic functions again? :-)

More practically, the undocumented simplegeneric decorator in pkgutil
could be used:

Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pkgutil import simplegeneric
>>> @simplegeneric
... def f(obj):
...    return "Object of type %s" % type(obj)
...
>>> def str_f(s):
...    return "String: " + s
...
>>> f.register(str, str_f)
<function str_f at 0x00B461F0>
>>> f("Test")
'String: Test'
>>> f(1)
"Object of type <type 'int'>"
>>>

To me, that seems better than inventing yet another special method.

Paul.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to