Dictionary keys are evolved for evaluation.?

_dict = {True: "True", not True: "not True", not True== False: "evaluation", False: "False", not False: "not False"}
for key in _dict:
    print(_dict[key])

How reasonable are the interpretation and indexing here…?

_dict = {True: "True", not True: "not True", not True== False: "evaluation", False: "False", not False: "not False"}

Keys and values are evaluated before the dict is created. In this case, they evaluate to:

_dict = {
    True: "True",
    False: "not True",
    True: "evaluation",
    False: "False",
    True: "not False",
}

When a dict is initialized with multiple identical keys, the ones which are mentioned later takes precedence:

>>> mydict = {"key": 1, "key": 2, "key": 3}
>>> print(mydict)
{'key': 3}

This means that your example is equivalent to:

_dict = {
    True: "not False",
    False: "False",
}

So the “interpretation and indexing” is perfectly reasonable. However, defining your keys like this is entirely unreasonable.

@abessman thanks for your answer, but I have the pretext of the evaluation and precedence. My concern is,

  1. why are the keys getting deduced by the evaluation of the condition, how can they be made?
    ex: what is ~7 == ?
  2. If then, to what extent can these be carried?

Don’t understand the question. How else would the keys be “deduced”? ~7 == -8.

What do you mean by “carried”?

Yes, of course dictionary keys are evaluated before being used as keys. It would be very annoying if they weren’t evaluated.

Your keys evaluate to:

  • True
  • not TrueFalse
  • not True == Falsenot FalseTrue
  • False
  • not FalseTrue

When you try to put the same key into a dict twice, the second one wins,
so you get the following dict:

{True: 'not False', False: 'False'}

How reasonable is this? Very reasonable.

1 Like

If the keys are not evaluated, they cannot be used.

You can find out yourself by using the Python interpreter. ~7 is the bitwise-not of 7, evaluated as a negative integer, which equals -8.

Can what be carried?

1 Like

The syntax for key:value pairs is expression : expression where expression is a fairly general term defined elsewhere. Key expresions are commonly literals or identifiers, but as you discovered, are not so limited. Expressions are evaluated left to right and the resulting object references used to create the dict.