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.
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.
It is not quite what you want to accomplish but I think the following solution is close to your goal. You could consider implementing a singleton. (taken from www.python.org)
class Singleton(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it
def init(self, *args, **kwds):
pass
Now you can test with:
>>> class MySingleton(Singleton):
... def init(self):
... print "calling init"
... def __init__(self):
... print "calling __init__"
...
>>> x = MySingleton()
calling init
calling __init__
>>> assert x.__class__ is MySingleton
>>> y = MySingleton()
calling __init__
>>> assert x is y
>>>
You could modify this example to raise an ex or a warning if somebody tries to reassign it. Also if you want your singleton attributes to be immutable you could consider using tuples or namedtuples to accomplish this.
C? Or do you mean reassigning the nameCto refer to a different value?