0

What is the semantics of a Python 2.7 line containing ONLY identifier. I.e. simply

a

or

something

?

If you know the exact place in the Reference, I'd be very pleased. Tnx.

4
  • 1
    The short answer is it doesn't do anything (except possibly cause a NameError if e.g. a isn't defined). Commented Nov 19, 2014 at 15:11
  • @jonrsharpe But I didn't know it and it was exactly what I needed. I saw the code try: ip1_i_im1 except NameError: ip1_i_im1 = "None" for the first time in my life. Commented Nov 19, 2014 at 15:24
  • That seems like a hack to me - much easier to assign ip1_1_im1 = "None" before whatever could fail to assign it. Commented Nov 19, 2014 at 15:26
  • Well, probably. But the author of the script I'm getting through decided it's more convenient method to find out if the variable was created. Anyway, through THIS METHOD he pushed me to finding something new about Python. Commented Nov 19, 2014 at 15:33

2 Answers 2

3

An identifier by itself is a valid expression. An expression by itself on a line is a valid statement.

The full semantic chain is a little more involved. In order to have nice operator precedence, we classify things like "a and b" as technically both an and_test and an or_test. As a result, a simple identifier technically qualifies as over a dozen grammar items

stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
             import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
                     ('=' (yield_expr|testlist_star_expr))*)
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
test: or_test ['if' or_test 'else' test] | lambdef
or_test: and_test ('or' and_test)*
and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
expr: xor_expr ('|' xor_expr)*
xor_expr: and_expr ('^' and_expr)*
and_expr: shift_expr ('&' shift_expr)*
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
term: factor (('*'|'/'|'%'|'//') factor)*
factor: ('+'|'-'|'~') factor | power
power: atom trailer* ['**' factor]
atom: ('(' [yield_expr|testlist_comp] ')' |
       '[' [testlist_comp] ']' |
       '{' [dictorsetmaker] '}' |
       NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')

a stmt can be composed of a single simple_stmt, which can be composed of a simgle small_stmt, which can be composed of a single expr_stmt, and so on, down through testlist_star_expr, test, or_test, and_test, not_test, comparison, expr, xor_expr, and_expr, shift_expr, arith_expr, term, factor, power, atom, and finally NAME.

Sign up to request clarification or add additional context in comments.

Comments

1

It's a simple expression statement: https://docs.python.org/2/reference/simple_stmts.html

1 Comment

Almost there! More precisely, what I was searching for was section 5.2.1. Identifiers (Names) and the line "When the name is bound to an object, evaluation of the atom yields that object. When a name is not bound, an attempt to evaluate it raises a NameError exception."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.