472,353 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Best practise implementation for equal by value objects

Hi,

I am new here and relatively new to Python, so be gentle:

Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?

Example:

class Age:

def __init__(self, an_age):
self.age = an_age

def __eq__(self, obj):
self.age == obj.age

def __repr__(self):
return self.__class__.__name__ + \
"(%r)" % self.age

age_ten = Age(10)
print repr(age_ten)
print eval(repr(age_ten))
print eval(repr(age_ten)).age

Running this gives

Age(10)
Age(10)
10

Exactly as I want to.

The problem arises when the Age class is iomported into another module
in another package as then there is a package prefix and the above
implementation of __repr__ does not work.

I have then experimented with doing somthing like

def __repr__(self):
return self.__module__ + '.' + self.__class__.__name__ +
"(%r)" % self.age

This seems to work when called from the outside, but not from the
inside of the module. That is, if I rerun the script above the the
module name prefixed to the representation I get the following error

Traceback (most recent call last):
File "valuetest.py", line 15, in <module>
print eval(repr(age_ten))
__main__.Age(10)
File "<string>", line 1, in <module>
NameError: name '__main__' is not defined

This is pretty annoying.

My question is: Is there a robust generic type of implementation of
__repr__ which I can use instead?

This is something I plan to reuse for many different Value classes, so
I would like to get it robust.

Thanks,
Slaunger
Aug 6 '08 #1
12 1994


Slaunger wrote:
Hi,

I am new here and relatively new to Python, so be gentle:

Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?
The CPython implementation gives up on that goal and simply prints
<modname.classname object at addressfor at least two reasons ;-).

1. In general, it require fairly sophisticated analysis of __init__ to
decide what representation of what attributes to include and decide if
the goal is even possible. If an attribute is an instance of a user
class, then *its* __init__ needs to be analyzed. If an attribute is a
module, class, or function, there is no generic evaluable representation.

2. Whether eval(repr(x)) even works (returns an answer) depends on
whether the name bindings in the globals and locals passed to eval
(which by default are the globals and locals of the context of the eval
call) match the names used in the repr. You discovered that to a first
approximation, this depends on whether the call to repr comes from
within or without the module containing the class definition. But the
situation is far worse. Consider 'import somemod as m'. Even if you
were able to introspect the call and determine that it did not come from
somemod**, prepending 'somemod.' to the repr *still* would not work.
Or, the call to repr could come from one context, the result saved and
passed to another context with different name bindings, and the eval
call made there. So an repr that can be eval'ed in any context is hopeless.

If this is a practical rather than theoretical question, then use your
first repr version that uses the classes definition name and only eval
the result in a context that has that name bound to the class object.

from mymod import Age
#or
import mymod
Age = mymod.Age

#in either case
eval(repr(Age(10))) == Age(10)
class Age:

def __init__(self, an_age):
self.age = an_age

def __eq__(self, obj):
self.age == obj.age

def __repr__(self):
return self.__class__.__name__ + \
"(%r)" % self.age
**
While such introspection is not part of the language, I believe one
could do it in CPython, but I forgot the details. There have been
threads like 'How do I determine the caller function' with answers to
that question, and I presume the module of the caller is available also.

Terry Jan Reedy

Aug 6 '08 #2

On Wed, 2008-08-06 at 05:50 -0700, Slaunger wrote:
Hi,

I am new here and relatively new to Python, so be gentle:

Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?

Example:

class Age:

def __init__(self, an_age):
self.age = an_age

def __eq__(self, obj):
self.age == obj.age

def __repr__(self):
return self.__class__.__name__ + \
"(%r)" % self.age

age_ten = Age(10)
print repr(age_ten)
print eval(repr(age_ten))
print eval(repr(age_ten)).age

Running this gives

Age(10)
Age(10)
10

Exactly as I want to.

The problem arises when the Age class is iomported into another module
in another package as then there is a package prefix and the above
implementation of __repr__ does not work.

I have then experimented with doing somthing like

def __repr__(self):
return self.__module__ + '.' + self.__class__.__name__ +
"(%r)" % self.age

This seems to work when called from the outside, but not from the
inside of the module. That is, if I rerun the script above the the
module name prefixed to the representation I get the following error

Traceback (most recent call last):
File "valuetest.py", line 15, in <module>
print eval(repr(age_ten))
__main__.Age(10)
File "<string>", line 1, in <module>
NameError: name '__main__' is not defined

This is pretty annoying.

My question is: Is there a robust generic type of implementation of
__repr__ which I can use instead?

This is something I plan to reuse for many different Value classes, so
I would like to get it robust.

Thanks,
Slaunger
--
http://mail.python.org/mailman/listinfo/python-list
Are you really sure this is what you want to do, and that a less tricky
serialization format such as that provided by the pickle module wouldn't
work for you?

--
John Krukoff <jk******@ltgc.com>
Land Title Guarantee Company

Aug 6 '08 #3
On Wed, 06 Aug 2008 05:50:35 -0700, Slaunger wrote:
Hi,

I am new here and relatively new to Python, so be gentle:

Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?
In general, no.

....
My question is: Is there a robust generic type of implementation of
__repr__ which I can use instead?

This is something I plan to reuse for many different Value classes, so I
would like to get it robust.
I doubt you could get it that robust, nor is it intended to be.

eval(repr(obj)) giving obj is meant as a guideline, not an invariant --
there are many things that can break it. For example, here's a module
with a simple class:
# Parrot module
class Parrot(object):
def __repr__(self):
return "parrot.Parrot()"
def __eq__(self, other):
# all parrots are equal
return isinstance(other, Parrot)
Now let's use it:
>>import parrot
p = parrot.Parrot()
s = repr(p)
assert eval(s) == p
del parrot
assert eval(s) == p
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'parrot' is not defined
If you look at classes in the standard library, they often have reprs
like this:
>>repr(timeit.Timer())
'<timeit.Timer instance at 0xb7f14bcc>'

Certainly you can't expect to successfully eval that!

I believe the recommendation for eval(repr(obj)) to give obj again is
meant as a convenience for the interactive interpreter, and even there
only for simple types like int or list. If you can do it, great, but if
it doesn't work, so be it. You're not supposed to rely on it, and it's
not meant as a general way to serialize classes.
--
Steven
Aug 7 '08 #4
On 6 Aug., 21:36, Terry Reedy <tjre...@udel.eduwrote:
Slaunger wrote:
Hi,
I am new here and relatively new to Python, so be gentle:
Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?

The CPython implementation gives up on that goal and simply prints
<modname.classname object at addressfor at least two reasons ;-).

1. In general, it require fairly sophisticated analysis of __init__ to
decide what representation of what attributes to include and decide if
the goal is even possible. *If an attribute is an instance of a user
class, then *its* __init__ needs to be analyzed. *If an attribute is a
module, class, or function, there is no generic evaluable representation.
OK, the situation is more complicated than that then. In the case here
though,
the attributes would always be sinmple bulit-in types, where
eval(repr(x))==x
or, where the attribute is a user-defined equal-by-value class, that I
have
control over.

The classes I am making as struct type classes with some added
functionlity for
human readable string representation, packing into a stream or
unpacking from a stream
using a "private" class Struct.

I come from a Java and JUnit world, where, if I am used to always
overriding the default reference based implementations of the
equals(), toString(),
and hashCode() methods for "equals-by-value" objects such that they
work well
and efficient in, e.g., hash maps.

With my swich-over to Python, I looked for equivalent features and
stumbled over the
eval(repr(x))==x recommendation. It is not that I actually (yet) need
the repr implementations,
but mostly because I find the condition very useful in PyUnit to check
in a test that I have remembered
to initialize all instance fields in __init__ and that I have
remembered to include all relevant
attributes in the __eq__ implementation.

Whereas this worked fine in a unit test module dedicated to only test
the specific module, the test failed
when called from other test package modules, wrapping the unit tests
from several unit test modules.
>
2. Whether eval(repr(x)) even works (returns an answer) depends on
whether the name bindings in the globals and locals passed to eval
(which by default are the globals and locals of the context of the eval
call) match the names used in the repr. *You discovered that to a first
approximation, this depends on whether the call to repr comes from
within or without the module containing the class definition. *But the
situation is far worse. *Consider 'import somemod as m'. *Even if you
were able to introspect the call and determine that it did not come from
somemod**, prepending 'somemod.' to the repr *still* would not work.
Or, the call to repr could come from one context, the result saved and
passed to another context with different name bindings, and the eval
call made there. *So an repr that can be eval'ed in any context is hopeless.
Ok, nasty stuff
If this is a practical rather than theoretical question, then use your
first repr version that uses the classes definition name and only eval
the result in a context that has that name bound to the class object.

from mymod import Age
#or
import mymod
Age = mymod.Age

#in either case
eval(repr(Age(10))) == Age(10)
class Age:
* * def __init__(self, an_age):
* * * * self.age = an_age
* * def __eq__(self, obj):
* * * * self.age == obj.age
* * def __repr__(self):
* * * * return self.__class__.__name__ + \
* * * * * * * *"(%r)" % self.age
Yes, it is most from a practicl point of view, altough I was surprised
that I could not find more material on it in the Python documentation
or mailing groups, and I moight just do what you suggest in the unit
test modules to at least make it robust in that context.

Hmm... a bit of a dissapointment for me that this cannot be done
cleaner
**
While such introspection is not part of the language, I believe one
could do it in CPython, but I forgot the details. *There have been
threads like 'How do I determine the caller function' with answers to
that question, and I presume the module of the caller is available also.
OK, I think CPython, for the moment, is too much new stuff to dig into
right now.
Just grasping some of all the possibilities in the API, and how to do
things the right way
is giving me enough challenges for now...
>
Terry Jan Reedy
Again, thank you for your thorough answer,

Slaunger
Aug 7 '08 #5
On 6 Aug., 21:46, John Krukoff <jkruk...@ltgc.comwrote:
On Wed, 2008-08-06 at 05:50 -0700, Slaunger wrote:
Hi,
I am new here and relatively new to Python, so be gentle:
Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?
Example:
class Age:
* * def __init__(self, an_age):
* * * * self.age = an_age
* * def __eq__(self, obj):
* * * * self.age == obj.age
* * def __repr__(self):
* * * * return self.__class__.__name__ + \
* * * * * * * *"(%r)" % self.age
age_ten = Age(10)
print repr(age_ten)
print eval(repr(age_ten))
print eval(repr(age_ten)).age
Running this gives
Age(10)
Age(10)
10
Exactly as I want to.
The problem arises when the Age class is iomported into another module
in another package as then there is a package prefix and the above
implementation of __repr__ does not work.
I have then experimented with doing somthing like
* * def __repr__(self):
* * * * return self.__module__ + '.' + self.__class__.__name__ +
"(%r)" % self.age
This seems to work when called from the outside, but not from the
inside of the module. That is, if I rerun the script above the the
module name prefixed to the representation I get the following error
Traceback (most recent call last):
* File "valuetest.py", line 15, in <module>
* * print eval(repr(age_ten))
__main__.Age(10)
* File "<string>", line 1, in <module>
NameError: name '__main__' is not defined
This is pretty annoying.
My question is: Is there a robust generic type of implementation of
__repr__ which I can use instead?
This is something I plan to reuse for many different Value classes, so
I would like to get it robust.
Thanks,
Slaunger
--
http://mail.python.org/mailman/listinfo/python-list

Are you really sure this is what you want to do, and that a less tricky
serialization format such as that provided by the pickle module wouldn't
work for you?
Well, it is not so much yet for serialization (although i have not yet
fully understood the implications), it is more because
I think the eval(repr(x))==x is a nice unit test to make sure my
constructor and equals method is implemented correctly (that I have
rememebered all attributes in their implementations).

As mentioned above, I may go for a more pragmatic approach, where i
only use repr if it "standard" imported

Cheers,
Slaunger
>
--
John Krukoff <jkruk...@ltgc.com>
Land Title Guarantee Company- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
Aug 7 '08 #6
On 7 Aug., 04:34, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Wed, 06 Aug 2008 05:50:35 -0700, Slaunger wrote:
Hi,
I am new here and relatively new to Python, so be gentle:
Is there a recommended generic implementation of __repr__ for objects
equal by value to assure that eval(repr(x)) == x independet of which
module the call is made from?

In general, no.

...
OK.
My question is: Is there a robust generic type of implementation of
__repr__ which I can use instead?
This is something I plan to reuse for many different Value classes, so I
would like to get it robust.

I doubt you could get it that robust, nor is it intended to be.

eval(repr(obj)) giving obj is meant as a guideline, not an invariant --
there are many things that can break it. For example, here's a module
with a simple class:
OK, I had not fully understood the implications of 'not' implementing
__repr__
such that eval(repr(x)) == x, so I just tried to make it work to make
sure
life would be easy for me and my object as I went further into the
Python jungle

As mentioned above, i also find the eval(repr(x))==x condition
convenient from
a unit test point of view.
>
# Parrot module
class Parrot(object):
* * def __repr__(self):
* * * * return "parrot.Parrot()"
* * def __eq__(self, other):
* * * * # all parrots are equal
* * * * return isinstance(other, Parrot)

Now let's use it:
>import parrot
p = parrot.Parrot()
s = repr(p)
assert eval(s) == p
del parrot
assert eval(s) == p

Traceback (most recent call last):
* File "<stdin>", line 1, in <module>
* File "<string>", line 1, in <module>
NameError: name 'parrot' is not defined
OK, I see, but this isn't exactly eval(repr(x))==x but
s = repr(x)
eval(s) == x

so, of course, is s is deleted in between it won't work.

In my implementation I only expect this should work as a one-liner.
If you look at classes in the standard library, they often have reprs
like this:
>repr(timeit.Timer())

'<timeit.Timer instance at 0xb7f14bcc>'
Yes, I noticed that. But the example here is also an object, which is
equal by reference, not value. And for these
it does not make so much sense to evaluate the representation.
Certainly you can't expect to successfully eval that!

I believe the recommendation for eval(repr(obj)) to give obj again is
meant as a convenience for the interactive interpreter, and even there
only for simple types like int or list. If you can do it, great, but if
it doesn't work, so be it. You're not supposed to rely on it, and it's
not meant as a general way to serialize classes.

--
Steven
OK, I will put less emphasis on it in the future.

Thank you for taking your time to answer.

Slaunger
Aug 7 '08 #7
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com.auwrote:
I believe the recommendation for eval(repr(obj)) to give obj again is
meant as a convenience for the interactive interpreter, and even there
only for simple types like int or list.

and even with types as simple as list and int the round tripping can easily
fail:
>>lst = [1, 2, 3]
lst[1] = lst
eval(repr(lst))
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
eval(repr(lst))
File "<string>", line 1
[1, [...], 3]
^
SyntaxError: invalid syntax

--
Duncan Booth http://kupuguy.blogspot.com
Aug 7 '08 #8


Slaunger wrote:
On 6 Aug., 21:36, Terry Reedy <tjre...@udel.eduwrote:
OK, the situation is more complicated than that then. In the case here
though,
the attributes would always be sinmple bulit-in types, where
eval(repr(x))==x
or, where the attribute is a user-defined equal-by-value class, that I
have
control over.
I think most would agree that a more accurate and informative
representation is better than a general representation like Pythons
default. For instance,
>>a=range(2,10,2) # 3.0
a
range(2, 10, 2)

is nicer than <class 'range' object at ######>.

So when the initializers for instances are all 'nice' (as for range), go
for it (as in 'Age(10)'). And test it as you are by eval'ing the rep.
Just accept that the eval will only work in contexts with the class name
bound to the class. For built-in like range, it always is, by default
-- unless masked by another assignment!

Terry Jan Reedy
Aug 7 '08 #9
Terry Reedy <tj*****@udel.eduwrites:
So when the initializers for instances are all 'nice' (as for range),
go for it (as in 'Age(10)'). And test it as you are by eval'ing the
rep. Just accept that the eval will only work in contexts with the
class name bound to the class. For built-in like range, it always is,
by default -- unless masked by another assignment!
Eval is extremely dangerous. Think of data from untrusted sources,
then ask yourself how well you really know where ALL your data came
from. It's preferable to avoid using it that way. There have been a
few "safe eval" recipes posted here and at ASPN. It would be good if
one of them made it into the standard library. Note that pickle
(which would otherwise be an obious choice for this) has the same
problems, though not as severely as flat-out evalling something.
Aug 7 '08 #10
On 7 Aug., 21:25, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Terry Reedy <tjre...@udel.eduwrites:
So when the initializers for instances are all 'nice' (as for range),
go for it (as in 'Age(10)'). *And test it as you are by eval'ing the
rep. Just accept that the eval will only work in contexts with the
class name bound to the class. *For built-in like range, it always is,
by default -- unless masked by another assignment!

Eval is extremely dangerous. *Think of data from untrusted sources,
then ask yourself how well you really know where ALL your data came
from. *It's preferable to avoid using it that way. *There have been a
few "safe eval" recipes posted here and at ASPN. *It would be good if
one of them made it into the standard library. *Note that pickle
(which would otherwise be an obious choice for this) has the same
problems, though not as severely as flat-out evalling something.
Thank you for pointing out the dangers of eval. I think you are right
to
caution about it. In my particular case it is a closed-loop system, so
no
danger there, but that certainly could have been an issue.

That caution should perhaps be mentioned in
http://docs.python.org/lib/built-in-funcs.html
Aug 8 '08 #11
On 7 Aug., 21:19, Terry Reedy <tjre...@udel.eduwrote:
Slaunger wrote:
On 6 Aug., 21:36, Terry Reedy <tjre...@udel.eduwrote:
OK, the situation is more complicated than that then. In the case here
though,
the attributes would always be sinmple bulit-in types, where
eval(repr(x))==x
or, where the attribute is a user-defined equal-by-value class, that I
have
control over.

I think most would agree that a more accurate and informative
representation is better than a general representation like Pythons
default. *For instance,
*>>a=range(2,10,2) # 3.0
*>>a
range(2, 10, 2)

is nicer than <class 'range' object at ######>.

So when the initializers for instances are all 'nice' (as for range), go
for it (as in 'Age(10)'). *And test it as you are by eval'ing the rep.
Just accept that the eval will only work in contexts with the class name
* bound to the class. *For built-in like range, it always is, by default
-- unless masked by another assignment!
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.

Now, I just need to figure out if this is right and how to specify the
globals and locals if that is not too cumbersome...
or maybe I am just over-engineering...
Aug 8 '08 #12
On Fri, 08 Aug 2008 00:28:02 -0700, 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.

Now, I just need to figure out if this is right and how to specify the
globals and locals if that is not too cumbersome... or maybe I am just
over-engineering...

I think it is infeasible for the repr() of an object to know where it was
imported from. Including the globals and locals in the call to eval()
won't help you, because they can have changed between the time you
created the instance and the time you call repr(). Consider:

>>import datetime
x = datetime.time(20, 21, 22)
x
datetime.time(20, 21, 22)
>>eval(repr(x)) == x
True

So far so good! But now watch this, starting in a fresh session:
>>import datetime as timedate
t = timedate.time
timedate.tttime = timedate.time
del timedate.time
assert t is timedate.tttime

x1 = t(20, 21, 22)
x2 = timedate.tttime(20, 21, 22)
assert x1 == x2
What should repr(x1) and repr(x2) be, for your invariant eval(repr(x))==x
to hold?

It gets better (or worse):
>>alist = [None, t, None]
del t, timedate
x3 = alist[1](20, 21, 22)
assert x1 == x2 == x3
What should the repr() of x1, x2, x3 be now?
Bringing this back to the unittests... as I see it, there's an easy way
to solve your problem. In the unittest, just do something like the
following:

# repr(x) looks like "module.class(arg)",
# but we actually import it as package.module.submodule.class
module = package.module.submodule
assert eval(repr(x)) == x

I think that is all you need to do.
--
Steven
Aug 8 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to...
10
by: Barry Morris | last post by:
Hi I am a php newbie although I have been a programmer for years, this can be dangerous because all the languages I know use = as equal...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component"...
41
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.