[issue38769] generators are currently hashable

Ammar Askar report at bugs.python.org
Mon Nov 11 17:47:47 EST 2019


Ammar Askar <ammar at ammaraskar.com> added the comment:

An easier way to understand might be to just write out the generator expression "manually":

  def generator_expression():
    for k, v in sorted(self.__dict__.items()):
      yield k
  return hash(my_generator_expression())

Would you say that `generator_expression` here is mutable or immutable? The code that underpins how it generates values is clearly immutable just like functions but the values it closes over can be changed.

Another minimal example:

>>> def f():
...   yield from some_list
...
>>> some_list = [1,2,3]
>>> list(f())
[1, 2, 3]
>>> some_list = [1,2,3,4,5]
>>> list(f())
[1, 2, 3, 4, 5]

Would you say that `f` is immutable or mutable in this case?

----------
nosy: +ammar2

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38769>
_______________________________________


More information about the Python-bugs-list mailing list