当我知道我不应该将元素添加到元组时

Cor*_*rey 5 python tuples

Dive Into Python是众多消息来源之一:

您无法将元素添加到元组.

但看起来好像我被允许这样做.我的代码:

from string import find

def subStringMatchExact(target, key):
    t = (99,)
    location = find(target, key)
    while location != -1
        t += (location,)    # Why is this allowed?
        location = find(target, key, location + 1)
    return t

print subStringMatchExact("atgacatgcacaagtatgcat", "tg")
Run Code Online (Sandbox Code Playgroud)

输出:

(99, 1, 6, 16)
Run Code Online (Sandbox Code Playgroud)

这让我相信在初始化时我实际上并没有创建元组t.有人可以帮我理解吗?

man*_*nji 12

你在一个新元组中连接2个元组.您没有修改原件.

> a = (1,)
> b = a
> b == a
True
> a += (2,)
> b == a
False
Run Code Online (Sandbox Code Playgroud)