0

I need a clarification on the semantics of Python.

Is it true that when Python encounters a statement of the form

x = some_term

a new object is being created, as specified by the process of evaluation of the term some_term?

Now suppose that the following lines have been encountered by Python:

x = the_term 

y = the_term

Where the_term is some fixed term (assume that it does not contain any references to functions generating "random objects"). As I see this the behaviour of Python should be the following: first a new object O1 is created and associated with the name x. Then a second new object O2 is created and associated with the name y. Both names x and y are now in suitable namespace. Objects O1 and O2 are distinct objects, the are occupying different places in memory, and the got the same values. Hence the test

x == y 

should return True, and test

x is y

should return False.

So tell me please why do I observe the following:

x=5

y=5

now x==y returns (of course) True, and x is y returns True.

But if we put:

x=99999999999999999999

y=99999999999999999999

now x==y returns (of course) True, and x is y returns False.

Could you tell me what is happening? What is the "real" semantics of Python (since the one I have described is clearly wrong, as these examples show)?

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
Godot
  • 109
  • 1
  • 1
    dupe of http://stackoverflow.com/q/306313/1175053 – C S Jun 03 '16 at 14:56
  • And in case the 'duplicate' comments aren't clear, it's especially the `Python caches small integer objects` part – Simon Fraser Jun 03 '16 at 14:56
  • some more details can be found here http://stackoverflow.com/questions/6101379/what-happens-behind-the-scenes-when-python-adds-small-ints – miraculixx Jun 03 '16 at 15:00
  • I'm pretty sure the range of integers it caches is a system dependent thing. On my system running Python 2.7.11, it caches the values `-5` to `256` inclusive. – pzp Jun 03 '16 at 15:00
  • 1
    Well, the misunderstanding here is broader than simply Python caching integers. It is *far* from universally true that `x = some_term` creates a new object; most of the time, it will simply add a new reference to whatever `some_term` is. – Daniel Roseman Jun 03 '16 at 15:02
  • So is the proper semantics something like: "create a different object unless "the_term" evaluates to something small, in that case check if that small object exists, if not - create it, and if it does exist just make the used name refer to that object"? The questions you are giving links to are explaining some things, but none of them gives the correct semantics of the aspects of Python I am interested in. – Godot Jun 03 '16 at 15:03
  • 1
    note that in general your assumption is wrong - assignment does not equal allocation. e.g. `a = 5; b = a` would make `b` point to the same object as `b`. In Python variables are names that reference an object, not the object themselves. check this out https://www.youtube.com/watch?v=_AEJHKGk9ns – miraculixx Jun 03 '16 at 15:06
  • @miraculixx Sure, please feel free to provide the proper semantics then :) Moreover isn't a name usually a reference to anobject rather than object itself?:) – Godot Jun 03 '16 at 15:09
  • 2
    He just has. Assignment creates an additional reference to an object, it does not create a new one. – Daniel Roseman Jun 03 '16 at 15:11
  • @Daniel no he hasn't. It is just a semantics of instructions of the type "x=term", where "term" is just a simple term, i.e., a name. What if it involves some operational symbols or derived functional symbols? It is not that simple. To put it in other context: that "term" may denote a derived operation, that is clearly NOT in any namespace. – Godot Jun 03 '16 at 15:14
  • 2
    Makes no difference. Assignment will itself never create a new anything. The expression might return a new object, which will then be referenced by that name, but the creation is the responsibility of the expression, not the assignment. If the expression returns an existing object which is already associated with another name, then `newname is oldname` will be True. – Daniel Roseman Jun 03 '16 at 15:18
  • sorry, SO comments are neither the place nor the means to teach language semantics. please check out the video reference above, it goes to great length to sched light on the very question. – miraculixx Jun 03 '16 at 15:21
  • Also related: [Python: Different results when using PyCharm and IDLE/python](http://stackoverflow.com/questions/36999976/python-different-results-when-using-pycharm-and-idle-python) My answer there explains when `x=99999999999999999999 ; y=99999999999999999999` will refer to the same object or not. I also expand on "What is constant in python" in [my answer here](http://stackoverflow.com/a/37172897/5827215) both may be useful to you. – Tadhg McDonald-Jensen Jun 03 '16 at 15:21
  • @miraculix I totally agree. That is why I asked that question, so if you can please provide an extensive and informative answer. I'll be delighted to learn :) – Godot Jun 03 '16 at 15:24
  • OK then. One of my questions have been answered elsewhere. But clearly my other question (the one regarding the correct semantics) have not been answered. Therefore marking my question as a duplicate is quite wrong. – Godot Jun 03 '16 at 15:27
  • 1
    @Godot what exactly is your question about "correct semantics" ? Remember that there are multiple implementations of python so the exact implementation details are not guaranteed. – Tadhg McDonald-Jensen Jun 03 '16 at 15:31
  • @Tadhg I am just interested in a high-level description of what is happening when Python encounters instructions of the form I have mentioned. I don't need a technical or very detailed description, I will be more than happy to see a description on a conceptual level (akin to the one I have provided, which happens to be wrong as my examples prove:) If you can provide such a high-level low-detail operational semantics please do it, I will be grateful. I think my question have been misunderstood by others and incorrectly marked as duplicated question. – Godot Jun 03 '16 at 16:40
  • The definitive answer for "what is happening when Python encounters instructions" can nearly always be given my [`dis`](https://docs.python.org/2/library/dis.html#dis.dis), anything that simply uses a `LOAD_...` byte code or something that works up to it (calling a function that just loads something like `type(1)` will always return the same `int` object) Will retrieve an existing object where as anything else (I'm pretty sure) will create a new object. Does that perhaps answer your question? – Tadhg McDonald-Jensen Jun 03 '16 at 17:00
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113754/discussion-between-tadhg-mcdonald-jensen-and-godot). – Tadhg McDonald-Jensen Jun 03 '16 at 17:07

0 Answers0