Python Forum
When does Python detect Errors?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When does Python detect Errors?
#1
Hello, I am currently confused about the different times when errors are detected in Python...

First, for my understanding:
Python initially translates the written code into bytecode, and this time is called "compilation." Then, the code is interpreted line by line, which is the runtime. We cannot speak of a strict compilation like in Java in the case of Python. Have I understood everything correctly up to this point?

Now, for my next issue:
I don't quite understand when different errors are detected in Python.
For example, shouldn't a Syntax Error be detected during compilation? Because Python cannot translate grammatically incorrect code. However, my script states that Python recognizes Syntax Errors when the interpreter reaches the faulty line (and executes the code up to that point). Which one is correct?

As for static-semantic and dynamic-semantic errors, in my understanding, they should be detected during runtime. For instance, accessing an undefined variable does not involve a grammatical error, so the code can be translated without issues.

Thank you for any assistance!
Reply
#2
All python code is "executed" when the file is "compiled". First the source is converted into bytecodes, then the bytecodes are executed to define classes and functions and assign variables in the global scope. Syntax errors that result in not being able to parse the source code are caught in the conversion stage. Syntax errors like "this needs to be an expression" are caught in the second stage. Due to the dynamic nature of python, runtime errors like NameErrors, and AttributeErrors cannot be detected at this time. Python doesn't know what kind of object a function argument will be until the function gets called.

You can use a static analysis tool like mypy to catch a lot of the errors that would be caught by a traditional compiler. Consistently using type annotations helps with static analysis, and many IDE's can use the type information to identify potential problems and provide better code completion suggestions.

There is no substitute for testing. Python has great unit testing tools to help you detect runtime errors.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to detect abnormal data in big database python vanphuht91 5 1,155 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Rmarkdown opened by python code - errors Rav013 0 2,096 Apr-27-2021, 03:13 PM
Last Post: Rav013
  Python Request Errors PythonNoob1998 7 3,991 Jan-07-2021, 05:18 PM
Last Post: buran
  Python chess game to detect winner ddddd 1 2,013 Dec-13-2020, 10:24 PM
Last Post: michael1789
  How to detect the text above lines using OpenCV in Python pframe 0 2,525 Apr-14-2020, 09:53 AM
Last Post: pframe
  Pip Syntax Errors in CMD: Windows 10 and Python 3.8.1 jamesphopper 2 4,465 Feb-08-2020, 07:21 PM
Last Post: jamesphopper
  Python stops without errors shahgourav 4 2,784 Feb-04-2020, 11:44 PM
Last Post: micseydel
  Can the comments produce errors in python? newbieAuggie2019 9 4,309 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Detect finger clicks/snaps using Python fwinter102 0 2,050 Aug-12-2019, 04:02 PM
Last Post: fwinter102
  Running into errors when installing pip and pip3 on python 2.7 and python 3.6 tej7gandhi 1 2,853 May-05-2019, 10:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020