0

I honestly must be missing something.

So I have this nice syntax.

"Hello darling" #THIS!
print "Hello world!"

Why the hello darling doesn't throw syntax error. I mean, there's no print statement. The "Hello world" just sits there. And from the syntax highlight, the hello world is a string, not a comment.

This also works.

7
print "8"

And 7 is obviously an integer. The 7 just sits there, not referenced to a variable, just there. "Syntaxless"

Question: Why it works? Both 7 and "Hello Darling" is integer and string, but not comment. I mean, if this is a comment, well it make senses since the interpreter ignores comment, but this is not.

Realdeo
  • 449
  • 6
  • 19

4 Answers4

0

That is because you are creating those strings or ints and then not assigning them to any variable so python simply discards them after creating them.

For example, when you have

a = 5

python creates the int 5 and then assigns it to the variable a. However, if you just have

5

python creates the int 5 but then doesn't store it anywhere. The same goes for strings. Syntactically, it is valid. Semantically, it is a complete waste.

Hope that helps

sshashank124
  • 31,495
  • 9
  • 67
  • 76
0

It is not "syntaxless". The examples where you are just providing a literal conform to the defined syntax for python. They are just simple forms of Expression, and in python, any valid Expression is also a Statement.

Yes, the examples you gave are pointless ... in the sense that they have no influence on the result of the program. But more complicated Expressions can have side-effects when evaluated, and therefore can affect the result of the program.

Question: Why it works?

Because that's the way python was defined.

Why? Simplicity ... and so that you could run python interactively using a "repl" style interpreter loop. (Repl == "read, evaluate, print line")


By contrast, the Java programming language doesn't allow you to write "Hello" as a statement. But to achieve this, the grammar rules for Expression in Java are rather more complicated.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You have only created a string named "Hello Darling", which is OK (except that it is wasteful) since that is thrown out after executing the print "Hello World" which is a command.

Nobi
  • 1,113
  • 4
  • 23
  • 41
0

Syntax errors are only raised if the code you are trying to run contains mistakes that mean the Python interpreter cannot know what code to run. Code that ends up discarding the result is not a syntax error.

In both your cases, you specified perfectly correct syntax. There are no errors in the lines. Python doesn't care if they don't do anything logical.

Both result in executable code, both result in an expression that produces an object (a str or int) that is then not used and end up being discarded again.

Note that Python has a specific use for just a string on a line, if it is at the start of a function, class or module. Then the string is used, it is assigned as the docstring:

def foo():
    "I am a docstring"
    return 'bar'

print foo.__doc__
# prints I am a docstring

In CPython (the reference Python implementation), literal expressions by their own on a line that are otherwise unused are simply optimised away. Your "Hello darling" and 7 lines never make it into the compiled bytecode.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343