Is "abc" + 5 considered an expression in python?

106 views Asked by At

Since a expression in python is defined as "a combination of values, variables, and operators that evaluates to a single value".

If we take something like "abc" + 5. though it is a combination of values, variable and operators it doesn't evaluate to a single value ,it would give an error since you cannot add a string with a integer.

So would it be considered an expression why or why not?

3

There are 3 answers

0
juanpa.arrivillaga On BEST ANSWER

Yes, it is absolutely an expression. Others have linked to the official language reference, but if that doesn't suit you, you can ask the CPython parser itself, the an authority on what constitutes a Python expression:

>>> import ast
>>> print(ast.dump(ast.parse('"abc" + 5'), indent=2))
Module(
  body=[
    Expr(
      value=BinOp(
        left=Constant(value='abc'),
        op=Add(),
        right=Constant(value=5)))],
  type_ignores=[])

It is an expression that will always result in an exception being raised. But that is netiher here nor there.

Consider the following function:

import random

def foo():
    if random.random() >= 0.5:
        raise RuntimeError("Nah ah ah!")

Well, then, I hope we can all agree that:

foo()

Is an expression. Even though sometimes it will raise an error. Otherwise, would have to say "we don't know if that is an expression or not until it is called."

No. It is an expression. An expression that can raise a runtime error. Like many other expressions.

Here is a good operational definition for an expression in Python. An expression in Python is anything that can go on the right-hand side of a simple assignment statement:

x = <some expression>
7
BoppreH On

I can't find an official source for that definition. Here's someone using it to explain Python, but here's someone using the exact same words to explain Javascript.

The official docs don't have such language when describing an expression.

And yes, "abc"+5 must be an expression, because "expression" is a term for a part of Python's grammar. It concerns values during parsing, regardless of their runtime behavior.

0
Silvio Mayolo On

Yes, by any reasonable definition. I'll argue this, both from your definition and from the one I would use.

According to your definition,

a expression in python is defined as "a combination of values, variables, and operators that evaluates to a single value".

I disagree with this, because it excludes literally everything. Your exception problem isn't unique to that example. a + b can never be an expression because a might be a class whose __add__ just throws. So I would, more precisely, say

An expression is a term in Python that evaluates to a single value or throws an exception

And that solves your problem.

But I still wouldn't use that definition. See, when we're talking about expressions, we're usually talking about syntax and parsing, and you're getting bogged down in the semantics of the runtime with your definition. Usually, we define an "expression" inductively. That is, we start with a few basic expressions (This is not a complete list, just some examples):

  • Every integer literal is an expression.
  • The special constant None is an expression.
  • A string literal enclosed in quotes is an expression.

Then we build up compound expressions inductively through conditionals.

  • If a and b are arbitrary expressions, then so is a + b.
  • If f and a1, a2, ... an are expressions, then so is f(a1, a2, ... an).
  • If a1, a2, ... an are expressions, then so is [a1, a2, ... an].

Such a definition ends up being largely technical, and the exact details are available publicly with the Python grammar. But again, that's really technical stuff, and you don't really need to know it unless you're hacking on the Python compiler itself.