[Python] How can a function find the function that called it?

Chris Gonnerman chris at gonnerman.org
Fri Dec 24 11:51:03 EST 2010


On 12/24/2010 10:24 AM, kj wrote:
> I want to implement a frozen and ordered dict.
>
> I thought I'd implement it as a subclass of collections.OrderedDict
> that prohibits all modifications to the dictionary after it has
> been initialized.
>
> In particular, calling this frozen subclass's update method should,
> in general, trigger an exception ("object is not mutable").
>
> But OrderedDict's functionality *requires* that its __init__ be
> run, and this __init__, in turn, does part of its initialization
> by calling the update method.
Use a flag, "private" to your new class, to indicate whether 
initialization is complete or not; your update method would see that 
initialization is not yet complete when called by __init__, and so it 
would do its business (calling the class method).  At the end of the 
__init__ function, set the initialized property to true.  If your update 
is called with the initialized property already set to true, it will 
raise the exception.




More information about the Python-list mailing list