Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When the Python interpreter deals with a .py file, is it different from dealing with a single statement?

Running:

a = 257
b = 257
print id(a) == id(b)

Results in:

same statement enter image description here

Same statement but opposite results. Why?

like image 623
Abirdcfly Avatar asked Apr 07 '16 11:04

Abirdcfly


People also ask

What does Python interpreter do?

The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.

Which interpreter is used when you run the command a PY?

The py Command Linux/Mac references the Python interpreter using the command python. Similar to the Windows py command, you can print out the version using the -V option. To run our script, you can use the Python interpreter command and point it to the script.

What are two ways to run a program using the Python interpreter?

There are two ways to use the python interpreter: interactive mode and script mode.

How do I select a Python interpreter?

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Note: If the Python extension doesn't find an interpreter, it issues a warning.

What is a Python interpreter?

Python interpreter is software logic between code and computer hardware. It takes a python program (like *.py file ) and run the program line by line from start to bottom. If the program require any computer HW for example Disk File, interpreter takes care of interaction with computer HW and it will be completely transparent to the python user.

Is it true that Python has to be interpreted?

Although there is nothing in the documentation that says that Python must be interpreted; the documentation simply describes CPython as the Python interpreter. An interpreter reads the source code line by line, executes that line, and moves on to the next; a loop or function ca

What is the difference between Py and PYC in Python?

You are in the right place. Files with extension .py contain Python code that is human readable. On the other side .pyc files contain bytecode that is not human readable. Files with .py extension are compiled into .pyc files that are then processed by the Python interpreter.

Is it possible to write multiline code in Python interpreter?

Ans 1. Yes. Python interpreters do allow writing multiline codes like if-else blocks, for loops, etc. An example of a multiline code written in interpreter is given below.


1 Answers

The code in test.py is parsed together which is more optimizable than the code parsed as separate statements in the interpreter

When you put it in a test.py and run it as a whole, the byte code compiler has a better chance of analyzing the usage of literals and optimizing them. (Hence you get a and b pointing to the same place)

As opposed to when you run the separate statements (parsed separately) in the interpreter (where I think it only optimizes up to 256 but not 257 via preallocation)

Play with this in the interpreter to see the effect of separate statements:

>>> a, b = 257, 257      # or if you prefer: a = 257; b = 257
>>> print a is b
True

>>> a = 257
>>> b = 257
>>> print a is b
False

Defining a function in the interperter also gives it a change to analyze and optimize the used literals

>>> def test():
...     a = 257
...     b = 257
...     print a is b
... 
>>> test()
True

This optimization is not limited to integers only, works e.g. for floats too (floats are not subject to a cache like integers in the [-5, 256] range are)

>>> def test():
...     pi = 3.14
...     x = 3.14
...     return x is pi
... 
>>> test()
True

# As opposed to separate statements:
>>> pi = 3.14
>>> x = 3.14
>>> x is pi
False

Looking at the byte code to see that it indeed reuses the same constant

>>> dis.dis(test)
  2           0 LOAD_CONST               1 (3.14)
              3 STORE_FAST               0 (pi)

  3           6 LOAD_CONST               1 (3.14) <-- Same constant 1 reused
              9 STORE_FAST               1 (x)

  4          12 LOAD_FAST                1 (x)
             15 LOAD_FAST                0 (pi)
             18 COMPARE_OP               8 (is)
             21 RETURN_VALUE  
like image 70
bakkal Avatar answered Oct 12 '22 20:10

bakkal