scared about refrences...

Tim Chase python.list at tim.thechases.com
Tue Oct 31 12:50:07 EST 2006


> I don't know how to make this structure immutable...  Pickle 
> it?  Seems very inefficient to me...

Well, classes can be made mostly immutable by intercepting the 
attempts to write to it...something like

class Foo(object):
     def __setattr__( self, name, val ):
         raise TypeError("I'm sorry, Dave.  You can't do that.")

It might also be feasible to do something like this with a 
decorator...?

> Every time I pass a variable now I will worry that it will be
> changed by the function... I haven't worried about things like
> this since the very early days of BASIC.... I don't know..
> maybe I have more to learn.

Well, there are at least solutions to this paranoia that occur to me:

-make a deep-copy of the object in question before calling the 
function, and then pass the copy to the function so that it can't 
alter the original

-trust/read the functions' documentation.  Most functions that 
alter the contents of their parameters are kind enough to note as 
much.  If functions go bunging with your parameters without 
documenting it, the function's coder needs to be smacked.

-tkc







More information about the Python-list mailing list