0

I saw a novel Python syntax,like this

test:[123,"abc"]
test:"123"

But is not a Assignment of variables.

print(test)


NameError Traceback (most recent call last) in ----> 1 print(test)

NameError: name 'test' is not defined

What does Python do in this step?

xin
  • 1

2 Answers2

0

what you saw probably was a dictionary like this :

exmaple_dict = {
   "test": [123, "abc"],
   "test2": "123"
}

see https://www.w3schools.com/python/python_dictionaries.asp for more information.

Amirreza Saki
  • 601
  • 7
  • 8
0

Contrary to another answer this is actually valid python code!

This is called a type-hint as specified in PEP-484.

Basically it tells the programmer and IDE what type a variable should be but has no effect at runtime.

When you only type test:"abc" (equal to test:str) it does not actually define test, so you get a NameError when you try to acces test later

wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29