Movatterモバイル変換
[0]ホーム
[Python-Dev] The purpose of the 'repr' builtin function
Peter Funkpf@artcom-gmbh.de
Tue, 11 Apr 2000 18:39:45 +0200 (MEST)
Hi![me:]> > or even more radical (here only for lists as an example):> >> > def __repr__(self):> > result = [self.__class__.__name__, "("]> > for item in self.data:> > result.append(repr(item))> > result.append(", ")> > result.append(")")> > return "".join(result) Guido van Rossum:> What's the advantage of this? It seems designed to be faster, but I> doubt that it really is -- have you timed it? I'd go for simple --> how time-critical can repr() be...?I feel sorry: The example above was nonsense. I confused 'str'with 'repr' as I quickly hacked the function above in. I erroneouslythought 'repr(some_list)' calls 'str()' on the items. If I only hadchecked more carefully before, I would have remembered that indeedthe opposite is true: Currently lists don't have '__str__' and sofall back to 'repr' on the items when 'str([....])' is used.All this is related to the recent discussion about the new annoyingbehaviour of Python 1.6 when (mis?)used as a Desktop calculator:Python 1.6a1 (#6, Apr 3 2000, 10:32:06) [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam>>> print [0.1, 0.2][0.10000000000000001, 0.20000000000000001]>>> print 0.10.1>>> print (0.1, 0.2)(0.10000000000000001, 0.20000000000000001)>>> print (0.1, 0.2)[0]0.1>>> print (0.1, 0.2)[1]0.2So if default behaviour of the interactive interpreter would be changednot to use 'repr()' for objects typed at the prompt (I believe TimPeters suggested that), this wouldn't help to make lists, tuples anddictionaries containing floats more readable.I don't know how to fix this, though. :-(Regards, Peter
[8]ページ先頭