Type annotation pitfall

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Sep 24 02:24:29 EDT 2021


On 24/09/21 5:48 pm, Robert Latest wrote:
> Never use mutable types in type hint,

No, the lesson is: Don't mutate a shared object if you don't want
the changes to be shared.

If you want each instance to have its own set object, you need to
create one for it in the __init__ method, e.g.

class Foo():
      x : set

      def __init__(self, s):
          self.x = set()
          if s:
              self.x.add(s)

-- 
Greg


More information about the Python-list mailing list