How can an int be '+' with a tuple?

Ben Finney ben+python at benfinney.id.au
Sat Jun 2 23:57:26 EDT 2018


Jach Fong <jfong at ms4.hinet.net> writes:

> The attached is a script

Thanks for making an example script. Instead of attaching it, please
post it along with your message so that everyone can read it. You can
make scripts suitable for posting in your message, by keeping them short
and simple <URL:http://sscce.org/>.

(For good reasons, attachments are dropped when messages are distributed
on the forum.)

> One thing make me puzzled is that the "any + context" at line
> 18. The "any" was passed as an integer from line 43 and the "context"
> was defined as a tuple at line 35. This concatenation works! how?

If the values are actually as you say, then Python should raise a
TypeError. For example::

    >>> 1 + ("foo", "bar")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

What makes me suspect that's different from your code, is that you say
“defined as a tuple”. Names in Python have no defined type; only an
object has a type, and operations (like ‘+’ only take effect on the
object, not the name.

-- 
 \     “I went to the cinema, it said ‘Adults: $5.00, Children $2.50’. |
  `\          So I said ‘Give me two boys and a girl.’” —Steven Wright |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list