0

If you go to a Python 3.8 console and type t:3 and then hit Enter, it won't do anything. It won't ever do anything if you type any valid variable name (whether the variable is defined or not), then a semicolon, then any expression. So, for example, any of these will work.

foo:"hello"
bar : 3
baz:['yes']
beans:[35]

or,

def foo():
    print('hi')
y:foo

None of these examples will produce any output, return any errors, or assign any variables.

I understand that you can put an expression like 1==1 or 69 as a statement in any program, and it won't do anything, but these examples make sense. They are expressions that make sense; they evaluate to something.

But this? foo:"bar"? Why can I do this? The only usages of colons I can think of are block delineation, dictionaries, and list slices. As far as I know, this is an incorrect usage of semicolons, and eval() agrees with me: if you try eval("foo:3") or anything like that, it will return a syntax error.

This feels like a huge mystery. What does this [variable name] : [expression statement mean??

How does Python interpret one of these [variable name]:[expression] statements? What does it think the semicolon does? Why isn't this a syntax error?

0 Answers0