Tuples and immutability
Joshua Landau
joshua at landau.ws
Sun Mar 9 15:57:48 EDT 2014
On 9 March 2014 18:13, Chris Angelico <rosuav at gmail.com> wrote:
> I think I see what you're saying here. But ignore "top-level"; this
> should just be a part of the exception message, no matter what.
I don't think I was clear, but yes. That.
> What you're saying is that this should notice that it's doing an
> augmented assignment and give some more text. This can be done; all
> you need to do is catch the error and reraise it with more info:
> [...]
> Now you can look at writing an import hook that does an AST transform,
> locating every instance of item assignment and wrapping it like that.
> It's certainly possible. I'm not sure how much benefit you'd get, but
> it could be done.
I would probably implement it closer to home. Inside
tuple.__getitem__, there would be something like
if context_is_augmented_assignment():
raise TypeError(message+warning)
else:
raise TypeError(message)
which would have much lower technical costs. It does depend on how
costly "context_is_augmented_assignment" is, though. A speed argument
is totally valid, but I'd hope it's possible to work around.
More information about the Python-list
mailing list