How Python does Compile-time and Run-time code checking?

Hi, I want to know How Python does Compile-time and Run-time code checking

I don’t even know if Jhon is reading these replies, or if he is even a
person, rather than a bot. Every question is the exact same format:

"Hi, I want to know "

and I don’t think I’ve ever seen Jhon respond, or acknowledge our
replies, or say thank you. Jhon, if you’re reading this, it would be
nice if you actually talked to us like a human being occasionally.

But for the benefit of anyone else reading this thread…

Python does no compile time code checking except to check if the code is
syntactically valid. So even code which is obviously meaningless will
compile, so long as it obeys the syntax rules:

(1.25).upper()  # Floats have no method "upper"

"spam and eggs".find(23)  # argument to "find" cannot be an int

x = 23 + None  # None does not support addition

The Python compiler is intentionally very simple-minded. It does very
little analysis of the code. However there exist a number of third-party
linters and other code-checkers which will check for many different
sorts of code issues, such as:

  • static (compile time) type errors
  • wrong number of arguments
  • impossible method calls
  • misspelled names
  • bad code style
  • compliance to coding styles
  • unused or dead code
  • test coverage
  • security flaws
  • code complexity
  • duplicate code

etc. Search for Python linters to see more:

At runtime, Python built-in functions are capable of doing full dynamic
type-checking, usually by attempting an operation and allowing it to
fail safely with an exception, rather than crashing the computer,
corrupting memory or having a segmentation fault. Usually you will get a
TypeError or AttributeError.

Some Python operations will test for error conditions ahead of time,
with isinstance, issubclass, or other runtime checks. And of course
code you write yourself can test for any condition you want.

To learn more about the difference between compile-time and run-time
type checking, you can read this:

1 Like