1

If I write this below two statements in a file sample.py

radius = 10

2*radius

and interpret(>>>python -i sample.py),

Why doesn't python stop with error at second statement (2*radius) which looks syntactically/semantically wrong and probably requires LHS(name) to bind the RHS value?

I see that neither Java nor C allowed such statements which are of no use and this has nothing to do with compiled/interpreted version.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • I am not sure about Java, but C as well allows you to write such statements. Just add `0;` or `42;` or `4+4;` or `printf("Hello world");` (the latter being an `int` expression as well) somewhere inside a function. All it invokes MIGHT be a compile time warning (in the case of the numbers, not with `printf()`). – glglgl Mar 26 '14 at 08:35
  • Strange thing: Java indeed doesn't allow this. But it allows function calls which discard the result. Quite inconsistent, IMO. – glglgl Mar 26 '14 at 08:39
  • Ya i think it is syntactically correct, but what about semantics, In python, it inturn calls (2).__mul__(10) which is object oriented approach – overexchange Mar 26 '14 at 08:52

4 Answers4

4

Python allows expressions on their own without their being part of a statement (assignment or otherwise).

If it didn't, then any in-place function or method call would require an assignment as well:

listobject.sort()  # this is nothing more than an expression

would have to be written as:

ignored = listobject.sort()  # assign None to ignored

Python cannot know that your expression has no effect on the rest of the code; radius could refer to an object with an .__rmul__ method, which would be called when Python executes your 2 * radius expression.

So, in the end, Python calculates 2 * 10 for you, discards the outcome, and moves on.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • martijn are you saying that python inturn interprets operators + * / % and invoke methods of left(2)/right(radius) operand(object) which can be .__mul__ or .__rmul__ if operator is *? so is the expression something like (2).__mul__(2,10) ? – overexchange Mar 25 '14 at 10:56
  • @Sham: Everything in Python is an object; yes, the Python `*` operator will use `.__mul__()` and `.__rmul__()` methods if present and not returning `NotImplemented` for a given argument. For `2 * 10` that could mean Python tries `(2).__mul__(10)` and if that returns `NotImplemented`, next tries `(10).__rmul__(2)`. – Martijn Pieters Mar 25 '14 at 11:04
  • Martijn ---- 1) Oh ok, so operators(+ * / %) are actually not primitive in python unlike Java or C. They are mapped to objects method as you mentioned in previous update, am i correct? ------ 2) Another point, 2 and radius(value 10) are internally considered two different integer type objects, What about operators, Are they just syntactic sugar to help python map * operator to .__mul__() method or .__rmul__() method? – overexchange Mar 25 '14 at 11:10
  • The `int` type has implemented a `__mul__` method (or rather, the [C-API equivalent](http://docs.python.org/2/c-api/typeobj.html#number-object-structures)); most operators are implemented to delegate to the objects involved instead, yes. – Martijn Pieters Mar 25 '14 at 11:14
  • Martijn --- Ok, this is interesting, Because pure object oriented style of computation. so if my python file has single line as 2(say), then internally what happens? – overexchange Mar 25 '14 at 11:17
  • @Sham: the constant `2` is loaded from the code object onto the stack. Then popped from the stack again as nothing happened with it. End program. – Martijn Pieters Mar 25 '14 at 11:18
  • martijn --- answer looks perfect to me, Can you also answer my query at [link](http://stackoverflow.com/questions/22604974/python-memory-model-for-this-program) am struggling to get perfect answer here – overexchange Mar 25 '14 at 11:31
  • martijn i have accepted, can u please respond to [link](http://stackoverflow.com/questions/22604974/python-memory-model-for-this-program) – overexchange Mar 25 '14 at 13:43
  • martijn i got answers for last 3 questions, but not for first one in query [link](http://stackoverflow.com/questions/22604974/python-memory-model-for-this-program) Do you think you can help me? i am unable to notify you. – overexchange Mar 26 '14 at 09:38
2

Python doesn't mind discarding the result of evaluating the expression. In fact, a docstring is merely a literal string that happens to be the first statement in a function. Such a string has no practical effect on the execution of the program.

200_success
  • 7,286
  • 1
  • 43
  • 74
1

It doesn't stop because python has no problem with it. It simply calculates 2*radius and then does nothing with the result and moves on through the rest of the code. Needless I say, it is a completely useless statement unless you are actually assigning the result to some variable

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • 1
    Also, if you use some tool like `pylint` against this type of code, you'd get *pointless statement* warning. – dmedvinsky Mar 25 '14 at 10:34
0

Because Python is not Java or C++, it allows that kind of expressions, and it indeed calculates it:

>>> radius = 10
>>> 2*radius
 20
jabaldonedo
  • 25,822
  • 8
  • 77
  • 77