0
class Foo(float):
   ...

C = Foo(1.23)

given a class/obj definition like this, is there any way for me to make C read-only? I want it to raise an error if C = ... is ever called after the object C is initialized.

2 Answers2

2

It's not possible. You can make the Foo object immutable but you can't make variables with references to it immutable. The object doesn't own the variable or have any control over it.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

No.

No matter what you assign to C, C itself is not an instance of whatever class you create. C is a variable. Operations that operate on the variable, like C = something_new, don't even look at your object except to decrement the refcount. You can define how operations on your object behave, but you can't redefine the semantics of the variable itself.

user2357112
  • 260,549
  • 28
  • 431
  • 505