-2

In the following example Python uses the same integer object in the memory for all three names x, y and z in the namespace

def main():
    x = 0
    y = 0
    z = 0
    print(x,y,z)
    print(x is y is z)
    y = 2
    print(x,y,z)
    print(x is y)
    print(z is y)
    print(x is z)

if __name__ == "__main__":main()

Output:

0 0 0
True
0 2 0
False
False
True

Why Python doesn't use the same tuple object here ( yes it is the choice of language designers but why) and a better questin to ask is when python creates new objects in memory

def main():
    a = 1, 2, 3
    b = (1, 2, 3)
    c = 1, 2, 3

    print(type(a) == type(b))
    print(a == b)
    print(a is b)
    print(a == c)
    print(a is c)

if __name__ == "__main__":main()

Output:

True
True
False
True
False
pouya
  • 3,400
  • 6
  • 38
  • 53
  • 3
    Can you please not dump pictures of code in your question? There's always the sterling option of just _pasting_ it. – cs95 Aug 18 '17 at 19:20
  • 2
    When Python creates a new object is implementation-dependent. In practice, string literals are reused, strings passed to `intern()` are reused, small integers are reused, the empty tuple is reused. There is a tradeoff between reusing all immutable objects, and keeping track of which immutable objects are available for reuse. – kindall Aug 18 '17 at 19:22
  • @cᴏʟᴅsᴘᴇᴇᴅ: The questioner knows the difference between `is` and `==`. The question is about why the tuples are different objects, not why `is` and `==` report different things. – user2357112 Aug 18 '17 at 19:24
  • @PM2Ring thanks man i will do from now on – pouya Aug 18 '17 at 19:30
  • Instead of doing it from now on, if you [edit](https://stackoverflow.com/posts/45763675/edit) now your question it will be more useful for future visitors. – ayhan Aug 18 '17 at 19:52
  • @ayhan i removed the images – pouya Aug 18 '17 at 20:21

1 Answers1

2

For a literal that represents a mutable object, every evaluation of the literal must generate a new object.

For a literal that represents an immutable object, whether a new object is created comes down to arbitrary implementation details. It could have chosen to use the same tuple. It could use the same tuple in a future release, or in a different Python implementation. Your example actually does reuse tuples if you call main repeatedly, but different tuples for a, b, and c. You shouldn't rely on it being one way or another.

If you want to see the particular implementation details for this case, the CPython bytecode compiler ordinarily merges equivalent constants together in a code object (the mechanism involves a dict, with some extra work to keep things like 1.0 and 1 from merging), but the peephole optimization pass that precomputes the 3 tuples occurs in a postprocessing phase that doesn't have the deduplication machinery in place.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Thanks man i need the logic behind things it makes me a better coder actually i thought the same and i expected Python reuse the tuple in the memory i will accept this answer if there is no other answer – pouya Aug 18 '17 at 19:41