Why are tuples immutable?
Gerrit
gerrit at nl.linux.org
Tue Dec 14 11:16:43 EST 2004
Fredrik Lundh wrote:
> > I could imagine that anything accepting numerical values for __getitem__
> > (foo[0], foo[1], ...) or that is iterable (foo.next(), foo.next()) could
> > be sensibly used as a formatting rhs. Of course, it is not compatible
> > because "foo %s" % [2, 4] is correct and "foo %s" % (2, 4) is not, but
> > are there other reasons that it has been chosen like this?
>
> so what should
>
> "foo %s bar %s" % "24"
>
> do?
Hmm. Good question (-:
Not possible before Python 3.0, but could __rmod__ play a role here?
str.__mod__ would be something like:
try:
# use other.__rmod__(...) result
except AttributeError:
try:
# use it as an array
except TypeError:
# use string representation
Hm, something else I don't understand in current behaviour:
>>> "foo".__rmod__("testing %s")
'testing foo'
>>> (55).__rmod__("testing %s")
NotImplemented
>>> print (55).__rmod__.__doc__
x.__rmod__(y) <==> y%x
...apparantly not? How should I interpret this?
To answer your question:
"foo %s bar %s" % "24" would do "24".__rmod__("foo %s bar %s") resulting in:
>>> "24".__rmod__("foo %s bar %s")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: not enough arguments for format string
Would it be possible to use something like this?
regards,
Gerrit.
--
Weather in Lulea / Kallax, Sweden 14/12 16:20:
2.0°C Broken clouds mostly cloudy wind 7.6 m/s S (34 m above NAP)
--
In the councils of government, we must guard against the acquisition of
unwarranted influence, whether sought or unsought, by the
military-industrial complex. The potential for the disastrous rise of
misplaced power exists and will persist.
-Dwight David Eisenhower, January 17, 1961
More information about the Python-list
mailing list