3

I'm new to the Python and following multiple online tutorials to learn. One of it is Google for Education.

In the Google's tutorial there is a section:

Code Checked at Runtime

Python does very little checking at compile time, deferring almost all type, name, etc. checks on each line until that line runs. Suppose the above main() calls repeat() like this:

def main():
if name == 'Guido':
    print repeeeet(name) + '!!!'
else:
    print repeat(name)

The if-statement contains an obvious error, where the repeat() function is accidentally typed in as repeeeet(). The funny thing in Python ... this code compiles and runs fine so long as the name at runtime is not 'Guido'. Only when a run actually tries to execute the repeeeet() will it notice that there is no such function and raise an error. This just means that when you first run a Python program, some of the first errors you see will be simple typos like this. This is one area where languages with a more verbose type system, like Java, have an advantage ... they can catch such errors at compile time (but of course you have to maintain all that type information ... it's a tradeoff).

There is a good example of runtime check in that section but no compile time check example.

And I'm interested in knowing about that little checking at compile time.

I can't find anything in the internet regarding that line. Every possible search returns about compiling python scripts and modules like this, this and this.


Edit:

python myscript.py is compiled(otherwise we wont be getting errors) and then interpreted to execute. Then compilation process should definitely produce a code(it might be byte-code). Is that code stored in memory instead of storing it as .pyc in the filesystem?


Edit 2:

For more on why the main script byte code is stored in memory and why modules are compiled can be found here.

SkrewEverything
  • 2,393
  • 1
  • 19
  • 50
  • You want to know what checks are done during compile time? or you want to know how to add compile checks? – Waman Jul 18 '17 at 05:28
  • 1
    Try to indent one line with 4 spaces and one line using `Tab`, you will get an error as soon as you run the program no matter where the lines are (no lines will run before that, that's how you know it was checked during compile and not during the run). This is the simplest example. Most of the compile errors are syntax errors. – Ofer Sadan Jul 18 '17 at 05:30
  • @Waman I want to know what checks are done during compile time. And does adding compile checks means compiling a script like `python3 -m py_compile some_script.py`? – SkrewEverything Jul 18 '17 at 05:32
  • Python is doing basic syntax checks and that's about it. – Grimmy Jul 18 '17 at 05:38
  • I am not sure about exact compiler procedure in python. but what "little check" here means on running python file it will check if all the modules used/imported are existing and have references but it won't check the variables or it's types. Because in python we don't use types to declare variables. So all such type errors are ignored in compile time and encountered only during execution.. – Waman Jul 18 '17 at 05:40
  • @Waman One more question. While I'm running the script, the python compiles it. Does it store that byte-code in the memory( java produces `.class` every time but I can't see `.pyc` or `.pyo` in the case of python) to later execute it directly? – SkrewEverything Jul 18 '17 at 05:45
  • A pyc file is created for imported modules, and they are placed in the same directory containing the py file. However... no pyc file is created for the main script for your program. In other words... if you call "python myscript.py" on the command line, there will be no .pyc file for myscript.py. – Waman Jul 18 '17 at 05:50
  • @Waman But `python myscript.py` is compiled(otherwise we wont be getting errors) and then interpreted to execute. Then compilation process should definitely produce a code(it might be byte-code). Is that code stored in memory instead of storing it as `.pyc` in the filesystem? – SkrewEverything Jul 18 '17 at 05:58
  • absolutely! it has to compile not stored in filesystem! reason behind is pretty simple, since it is main script the compiled pyc wont be reusable.. but if it is a module (without main) then the same pyc could be reused whenever it is imported.. – Waman Jul 18 '17 at 05:59

1 Answers1

1
  1. Not sure about exact compiler procedure in python. but what "little check" here means on running python file it will check if all the modules used/imported are existing and have references but it won't check the variables or it's types. Because in python we don't use types to declare variables. So all such type errors are ignored in compile time and encountered only during execution

  2. A pyc file is created for imported modules, and they are placed in the same directory containing the py file. However... no pyc file is created for the main script for your program. In other words... if you call "python myscript.py" on the command line, there will be no .pyc file for myscript.py. Since it is main script the compiled pyc wont be reusable.. but if it is a module (without main) then the same pyc could be reused whenever it is imported.

Hope it is useful!

Waman
  • 1,179
  • 7
  • 16