Python semicolon really matters

Why does using a colon matter for the result? And what should be the correct result?

# Not stored in a different location. >>> id('123 4')== id('123 4') True # Also returns true >>> x = '123 4'; y ='123 4'; id(x) == id(y) True 

But the same returns false.

 >>> x = '123 4' >>> y = '123 4' >>> id(x) == id(y) False 

The same in the function returns True

 >>> def test(): ... x = '123 4';y='123 4'; print (id(x)==id(y)) ... a = '123 4' ... b='123 4' ... print (id(a)==id(b)) ... >>> test() True True 
+8
python
source share
4 answers
 >>> x="123 4";y="123 4" 

Python is smart enough to recognize both variables, get the same value (since they are interpreted on the same line), and therefore save that value in the same memory location (i.e. id(x) == id(y) ).

but

 >>> x="123 4" >>> y="123 4" 

Python is not smart enough to understand that they have the same meaning (since they are interpreted on separate lines), and therefore saves them in its own memory location (i.e. id(x) != id(y) ).

+8
source share

This is just an accident of writing an interpreter. Doing the same in the script shows a different result. It seems to me that string interning occurs over compilation units.

(added stuff2.py file to display several modules)

stuff2.py:

 z = '123 4' 

stuff.py:

 x = '123 4';y='123 4';print id(x)==id(y) x = '123 4' y='123 4' print id(x)==id(y) import stuff2 print id(x)==id(stuff2.z) $ python stuff.py True True False 
+4
source share

Well, as this is obvious, this is due to the internationalization of strings in Python . This mechanism is implementation dependent, so any Python interpreter such as CPython , IronPython , PyPy , etc. can behave differently. And it can change between versions. It probably can even change between runs.

To solve your specific case, you need to analyze the source code of this version of your interpreter. Best of all, there is a slight difference in the implementation of processing statements passed on one line (separated by semicolons) and executing them one by one.

When working in interactive mode, you should keep in mind that quite a lot can happen between each line of code, because you can check it. When you pass everything at once, the interpreter is much less worried about what might happen between statements.

0
source share

If you assign an immutable object to a variable in python, the variable as well as the value points to the same location,

 >>> a = 5 >>> b = 5 >>> id(5) 11372376 >>> id(a) 11372376 >>> id(b) 11372376 >>> a == b True >>> a is b True 

Comparing identifiers will work exactly as you can see the ID values. Now try assigning variable objects to python variables.

 >>> x = '123 4' >>> y = '123 4' >>> x == y True >>> x is y False >>> id(x) 21598832 >>> id(y) 21599408 >>> id('123 4') 21599312 

Here you can see the difference in identifiers. how "is" compares values ​​with an address, where "==" compares directly with a reference value. However, it does not give an error in the case of immutable objects, since all points are in one place, but in the case of variability, since values ​​can change, variables are pointed to the current object and, therefore, give you a false result.

Hope this helps :)

0
source share

All Articles