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.

9
  • You want to know what checks are done during compile time? or you want to know how to add compile checks? Commented Jul 18, 2017 at 5: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. Commented Jul 18, 2017 at 5: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? Commented Jul 18, 2017 at 5:32
  • Python is doing basic syntax checks and that's about it. Commented Jul 18, 2017 at 5: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.. Commented Jul 18, 2017 at 5:40

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.