H
hg
Its ugly and boring.
It's rude, unnecessary _and_ boring
It's rude, unnecessary _and_ boring
Traceback (most recent call last):
Traceback (most recent call last):File said:
Sergey said:Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):(1,)+[1]
Traceback (most recent call last):File said:[1]+(1,)
Its ugly and boring.
[1, 1](1,)+tuple([1]) (1, 1)
[1]+list((1,))
[1, 1]L=[1]
L.extend((1,))
L
Sergey said:Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
(1,)+[1]
Traceback (most recent call last):
[1]+(1,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
Its ugly and boring.
Szabolcs said:[1, 1]L=[1]
L.extend((1,))
L
[1, 2]>>> L = [1]
>>> L += (2,)
>>> L
[1]+(1,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
Its ugly and boring.
Agreed. This would be similar to:
py> 1 + 1.0
Traceback: can only add int to int. Etc.
But then again, the unimaginative defense would be that it wouldn't be
python if you could catentate a list and a tuple.
Concatenating tuples and lists seems logical if you think of tuples as
sequences. If you think of them more like Pascal records or C structs
instead (I believe that's Guido's perspective on tuples) then it makes no
sense at all.
Skip
<type 'posix.stat_result'>>>> import os
>>> s = os.stat("/etc/hosts")
>>> s (33188, 34020475L, 234881029L, 1, 0, 0, 214L, 1170562950, 1124700602, 1142602578)
>>> s.st_mtime 1124700602.0
>>> s[0] 33188
>>> type(s)
But then again, the unimaginative defense would be that it wouldn't be
python if you could catentate a list and a tuple.
Steven said:Since lists and tuples are completely different objects with completely
different usages, what should concatenating a list and a tuple give?
Should it depend on the order you pass them?
1.0 + 1 == 1 + 1.0 for very good reasons: we consider (for pragmatic
reasons to do with loss of significant digits) that floats coerce ints
into floats rather than the other way around. But what should lists and
tuples do?
From the Zen of Python:
"In the face of ambiguity, refuse the temptation to guess."
James> Then iterating over them makes no sense?
I agree that tuples are a bit schizophrenic. They really are sequences from
an implementation standpoint, but from a logical standpoint it's maybe best
not to think of them that way.
That said, this:
for x in (1,2,3):
pass
is a skosh faster (perhaps an immeasurably small skosh) than this:
for x in [1,2,3]:
pass
so people will probably continue to use tuples instead of lists in these
sorts of situations.
For an example of the struct-ness of a tuple consider the output of os.stat:
<type 'posix.stat_result'>import os
s = os.stat("/etc/hosts")
s (33188, 34020475L, 234881029L, 1, 0, 0, 214L, 1170562950, 1124700602, 1142602578)
s.st_mtime 1124700602.0
s[0] 33188
type(s)
It's effectively a tuple with field names. I don't know when the switch
occurred (it's in 2.2, as far back as my built interpreter versions
currently go), but back in the day os.stat used to return a plain old tuple.
I have no idea if the schizophrenic personality of tuples will improve with
drugs^H^H^H^H^H Python 3, but I wouldn't be at all surprised if it did.
Skip
Is that a guess or just common sense?
Do you guess with __add__ and __radd__?
Steven said:Sorry, is *what* a guess?
Conceptually, ints are a subset of floats (they certainly are in pure
mathematics). Automatic coercions from ints to floats makes sense;
automatic coercions the other way rarely do -- should you round up or
round down or truncate? What is right in one application is not right for
another.
>
Lists and tuples, on the other hand, are conceptually two distinct data
types. Adding a list to a tuple is no more sensible than adding a list to
a string -- just because they're both sequences doesn't mean adding them
together is meaningful.Do you guess with __add__ and __radd__?
No. If there is an obviously correct behaviour for addition (like with
ints and floats) then I coerce the objects appropriately. If there is no
obviously correct behaviour, I refuse to guess.
The user's expected behaviour for [1] + (1,) might be to return a list, or
it might be to return a tuple. Since there is no obviously correct
behaviour, the right thing to do is to refuse to guess.
The user's expected behaviour for [1] + (1,) might be to return a list, or
it might be to return a tuple. Since there is no obviously correct
behaviour, the right thing to do is to refuse to guess.
I guess we differ on what is obvious. This seems obvious to me:
[1] + (1,) => [1, 1]
(1,) + [1] => (1, 1)
simply becuase the operand on the left should take precendence because
its "__add__" is called and its "__add__" returns a list.
In essence, as
we know the obviously correct behavior for __add__ and __radd__, then it
would be the obviously correct behavior that the above would follow.
I would venture to guess that most people would intuitively consider the
above behavior correct,
simply because the information content
difference between a list versus a tuple is non-existent (outside of the
information that one is a list and the other a tuple). Why would their
types dictate coercion?
With ints and floats, as you point out, the
reasons that type dictates coercion are obvious and mathematical. Thus,
for tuples and lists, it wouldn't make sense to consider one type taking
precendence over the other, so we fall back to position, which seems to
be the common sense approach.
It's effectively a tuple with field names. I don't know when the switch
occurred (it's in 2.2, as far back as my built interpreter versions
currently go), but back in the day os.stat used to return a plain old
tuple.
I have no idea if the schizophrenic personality of tuples will improve
with
drugs^H^H^H^H^H Python 3, but I wouldn't be at all surprised if it did.
I guess we differ on what is obvious. This seems obvious to me:
[1] + (1,) => [1, 1]
(1,) + [1] => (1, 1)
Given Guido's assertions about the nature of the tuple, no it doesn't.James said:Then iterating over them makes no sense?
BJörn Lindqvist said:I guess we differ on what is obvious. This seems obvious to me:
[1] + (1,) => [1, 1]
(1,) + [1] => (1, 1)
I agreed with you up to this point. But this seems more obvious to me:
[1] + (1,) => [1, 1]
(1,) + [1] => [1, 1]
In other languages and situations, types are widened, 1 + 1.0 = 1.0 +
1.0. And list is "wider" than tuple.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.