8 ms·
If you write this: #!/usr/bin/python import time time.sleep(5) fdafadsfadsf It'll take 5 seconds to crash with a "NameError: name 'fdafadsfadsf' is no
by michaelt 10y ago
If you write this:
#!/usr/bin/python
import time
time.sleep(5)
fdafadsfadsf
It'll take 5 seconds to crash with a "NameError: name 'fdafadsfadsf' is not defined". Likewise, if you do this:
#!/usr/bin/python
import time
x = 1
time.sleep(5)
print "x is "+x
It'll take 5 seconds to crash with "TypeError: cannot concatenate 'str' and 'int' objects"
In a language like Java, these would be syntax errors, and they'd be picked up at compile time as they're illegal in Java's syntax.
I assume they're legal in Python's syntax, because for all the syntax checker knows, time.sleep() might define fdafadsfadsf and convert x into a string. So they're runtime errors in Python.
Of course, Java still has runtime exceptions - null pointer exceptions, for example.
- jasonpeacock 10y agoJava and Python are apples and oranges - one is a compiled language, the other is interpreted. Better to look at other interpreted languages like Ruby & Perl...and they have the same issue. It's a side-effect of using an interpreted language, but it also allows such languages to be self-modifying which can be very powerful when you need it.
- mmagin 10y agoPerl actually has a 'compilation' phase where some types of syntax errors are actually detected before it goes executing the code.
- technomancy 10y agoPython and Java are both compiled to bytecode and both interpreted as bytecode. It has absolutely nothing to do with compilers.
- mmagin 10y agoYep. What drives me even more crazy is things which are purely syntactically wrong, e.g. an else: clause with no if before it are only detected when the interpreter gets to that line.