Best practise implementation for equal by value objects

Terry Reedy tjreedy at udel.edu
Fri Aug 8 16:33:42 EDT 2008



Slaunger wrote:

> OK, i am encouraged to carry on my quest with the eval(repr)) for my
> 'nice' classes.
> I just revisited the documentation for eval and noticed that there are
> optional globals
> and locals name space variables, that one could specify:
> 
> http://docs.python.org/lib/built-in-funcs.html
> 
> Quite frankly I do not understand how to make use of these parameters,
> but it is my feeling
> that if I enforce a convention of always specifying the globals/locals
> parameter in a specific
> manner:
> assert eval(repr(x), globals, locals) == x
> would work independent of how I have imported the module under test.

I have no opinion as to whether you should or should not use eval beyond 
testing.  But I believe the following will work and teach a bit more 
too.  In one module, define 'mynice = {'Age':Age, ...}' for your nice 
classes (that module could import from other modules and use dotted 
names as needed).  Then eval(repr(x), mynice, mynice) should work 
anywhere (as long as mynice has been imported!).




More information about the Python-list mailing list