1

Is there any way to prevent a name from being bound, or a variable from being rebound? For instance, given a foo variable, is it possible to have an assignment operation raise an exception?

More concretely, provided we have a total control on foo's class, is the following session possible?

>>> foo = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WhateverError: foo cannot be bound

I know that it's possible to make class attributes read-only, or to use __slots__ to prevent from adding new members, but I want to know if the same could be achieved on free variables (as in, non-member variables). Besides, I assume that the left member of the assignment is a valid identifier.

I'm asking this question out of curiosity. There's nothing I'm trying to accomplish with this, so this is not an XY problem.

Right leg
  • 13,581
  • 5
  • 36
  • 68

0 Answers0