1

Look at this simple dictionary:

d = {'name': 'soroush', 'age': 25}

Suppose I want to assign a new value to d in a wrong way:

d['aaa'] : 'bbb' instead of d['aaa'] = 'bbb'

Python does nothing in this situation...If I print the dictionary I get the same content as before. I expected to get a syntax error.

Isn't it too buggy in a large application and something that python interpreter should warn instead of skipping? My second question is, does : cause this to be skipped?

d = {'name': 'soroush', 'age': 25}
print(d)
d['aaa']: 'bbb'
print(d)
martineau
  • 119,623
  • 25
  • 170
  • 301
S.B
  • 13,077
  • 10
  • 22
  • 49
  • 2
    `:` has a purpose in python. you're declaring a variable type to an object. see https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5 – Nicolas Gervais Jan 07 '21 at 17:59
  • 1
    That's a variable type annotation. – Barmar Jan 07 '21 at 17:59
  • 1
    While it would be nice if all typos resulted in syntax errors, you can't depend on it. But a decent IDE would probably put a squiggle under that. – Barmar Jan 07 '21 at 18:02
  • Great. Understood guys thanks. I should be more careful when I use a simple editor. – S.B Jan 07 '21 at 18:08

2 Answers2

4

This is not a syntax error or a bug, it's a feature ;)

Python 3.6 added support for type annotations for variables (see PEP 526 for details). Example:

x: int = 42

this is pretty redundant, because the type hinting system of python can figure out that 42 is an integer, but there are more useful situations, e.g. if you want to declare the type of a variable before you assign its contents

from typing import Optional

x: Optional[str] = None

so, if you do

d: 'bbb'

you declare that variable d is of type 'bbb', which of course is nonsense, but you can declare it anyways. You can even verify this on your terminal

>>> __annotations__
{'d': 'bbb'}

Now with your actual code

d['aaa'] : 'bbb'

Here you retrieve the value of key 'aaa' in your dictionary and assign a type to it. This doesn't do anything really (check __annotations__), but it's still valid syntax.

klamann
  • 1,697
  • 17
  • 28
  • 1
    Thank you it was complete and understandable. I got the idea of type hints...so when I put `d['aaa']` , python doesn't even try to get the value from this key right? I mean because it finds out that this is a type hint, it doesn't evaluate the left hand side at all ? – S.B Jan 07 '21 at 18:25
  • That's an interesting question actually... we can test this by defining a function `def f(): print('foo'); return 'aaa'`. Now if we have `d = {'aaa': 'bbb'}` and we call `d[f()]: int`, I see "foo" printed on my terminal, so the expression gets evaluated, even though there is no update to the `__annotations__` dict. There's also a section in PEP 526 on [Annotating expressions](https://www.python.org/dev/peps/pep-0526/#annotating-expressions), but it's not too helpful in this case. – klamann Jan 07 '21 at 23:07
1

Well it depends on your IDE. I use VScode and it gave me a underline saying: Type annotation not supported for this type of expression Pylance and also if u want to add a new value to the dict, you can just say:

d = {'name': 'soroush', 'age': 25}
print(d)
d['aaa']= 'bbb'#instead of the colon, add a equal
print(d)

So in total, it depends on your IDE Hope this make sense.

Srishruthik Alle
  • 500
  • 3
  • 14