472,371 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

eval(repr(object)) hardly ever works


I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.

For example it doesn't work for instances of the object class:

In [478]: eval(repr(object()))
------------------------------------------------------------
File "<string>", line 1
<object object at 0xf233e8>
^
SyntaxError: invalid syntax

It seems to work for types like integers and dictionaries and lists,
but not for much else.

Any thoughts?
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 13 '06 #1
9 3878

Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.

For example it doesn't work for instances of the object class:

In [478]: eval(repr(object()))
------------------------------------------------------------
File "<string>", line 1
<object object at 0xf233e8>
^
SyntaxError: invalid syntax

It seems to work for types like integers and dictionaries and lists,
but not for much else.

Any thoughts?
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
I don't think that repr() is for eval(). repr() is for outputting a
string that represent the object and is not ambiguous. Example: print
'foo' == print u'foo' but print repr('foo') != print repr(u'foo')

Sep 13 '06 #2

MatthewI understand that idea of an object's __repr__ method is to
Matthewreturn a string representation that can then be eval()'d back
Matthewto life, but it seems to me that it doesn't always work.

No, where it's convenient that property is desirable. It's not a
hard-and-fast rule though. Some objects (think open files) it would be
impossible (or nearly so) to generate a representation which captured the
state of the object. For others (basic types or containers of them), it's
pretty easy.

MatthewIt seems to work for types like integers and dictionaries and
Matthewlists, but not for much else.

MatthewAny thoughts?

Sure, if you want to save and restore objects, pickle or marshal them.
Don't rely on repr().

Skip
Sep 13 '06 #3
Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.

For example it doesn't work for instances of the object class:

In [478]: eval(repr(object()))
------------------------------------------------------------
File "<string>", line 1
<object object at 0xf233e8>
^
SyntaxError: invalid syntax

It seems to work for types like integers and dictionaries and lists,
but not for much else.
That's intentional. Would you have it return the code of all the methods
when you take the repr() of a class?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 13 '06 #4
On Wed 13 Sep 2006 10:38:03 AM EDT, Steve Holden wrote:
That's intentional. Would you have it return the code of all the methods
when you take the repr() of a class?
I don't think that would be required. Couldn't you return a string with
a call to the constructor inside? That's what sets.Set seems to do:

In [510]: from sets import Set

In [511]: s = Set()

In [512]: s.add('baloney')

In [513]: repr(s)
Out[513]: "Set(['baloney'])"

In [514]: eval(repr(s))
Out[514]: Set(['baloney'])
regards
Steve
PS: I read your python web programming book a few years ago.

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 13 '06 #5
Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.
when in doubt, read the language reference:

"If at all possible, this should look like a valid Python
expression that could be used to recreate an object with
the same value (given an appropriate environment). If this
is not possible, a string of the form "<...some useful
description...>" should be returned.

http://pyref.infogami.com/__repr__

</F>

Sep 13 '06 #6
In article <11*********************@i42g2000cwa.googlegroups. com>,
"Hardcoded Software" <ha****************@gmail.comwrote:
Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.

For example it doesn't work for instances of the object class:

In [478]: eval(repr(object()))
------------------------------------------------------------
File "<string>", line 1
<object object at 0xf233e8>
^
SyntaxError: invalid syntax

It seems to work for types like integers and dictionaries and lists,
but not for much else.
I don't think that repr() is for eval(). repr() is for outputting a
string that represent the object and is not ambiguous. Example: print
'foo' == print u'foo' but print repr('foo') != print repr(u'foo')
Right, but that eval() idea dies hard. The document excerpt
quoted in an earlier followup, for __repr__, now admits that it
might not be possible ... but then the documentation for __str__
right below it says "differs from __repr() in that it does not
have to be a valid Python expression". There's plenty of evidence
in the standard libraries that people understand these two functions,
but they certainly have arrived at that understanding from some
other route than reading the documentation.

Donn Cave, do**@u.washington.edu
Sep 13 '06 #7
Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.
Just to reinforce something Skip mentioned:

If you're looking for a way to serialize an object into a string, and
then later turn that string back into an object, you probably want the
pickle module (the marshal module is similar but is really intended for
internal use; unless you know exactly why you're picking one over the
other, using pickle is probably the right call).

http://docs.python.org/lib/module-pickle.html

Sep 13 '06 #8

Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.
[snip]
>
Any thoughts?
This is actually an interesting issue when you're working with Python
interpreters. For example, an object with a simple repr() which is code
to create an identical object is easy to use as a key in a dictionary.

I ran into this a while back when I was tinkering with IDLE's
auto-completion module, and ended up writing some code which checks if
an object's repr() is "reversible" or not.

- Tal
reduce(lambda m,x:[m[i]+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]

Sep 17 '06 #9

Matthew Wilson wrote:
I understand that idea of an object's __repr__ method is to return a
string representation that can then be eval()'d back to life, but it
seems to me that it doesn't always work.

For example it doesn't work for instances of the object class:

In [478]: eval(repr(object()))
------------------------------------------------------------
File "<string>", line 1
<object object at 0xf233e8>
^
SyntaxError: invalid syntax

It seems to work for types like integers and dictionaries and lists,
but not for much else.

Any thoughts?
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
You have to write the code to return a proper representation.

Sep 17 '06 #10

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

Similar topics

4
by: lawrence | last post by:
My company is leasing a server from Interland, which is a very large web hosting company. I assume Interland knows how to set up a BSD server with the usual add-ons, including PHP. But when I run...
23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
2
by: deko | last post by:
Has anyone been able to import data from Claris Works into Access 2002? Is it simply a matter of exporting the Claris Works data into a comma delimited file and mapping the fields? Thanks in...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
6
by: Gary | last post by:
I'm confused about the two ways of string copy below. The first one (two functions) works; the second doesn't. Can anybody explain the reason to me? Thanks a lot. #include <stdio.h> /*this one...
9
by: Dave | last post by:
Hi, I've been trawling the web for answer to my problem with no luck although I'm hardly alone it seems! Below is the generated source for an ASP page that posts a value called 'album' to...
18
by: Luminari | last post by:
I have two pages. Both of them are supposed to have one table on it with a fixed header. Both pages were tested in IE6. Structurally they are identical. Both have a <divthat wraps a <tablethat...
1
by: cmdolcet69 | last post by:
Pleae help im trying to help fix a bug in some software that uses the Dev Express 3rd party control for Reports.Had anyone ever used this product?
2
tlhintoq
by: tlhintoq | last post by:
So I was setting up guidelines in the text editor of Visual Studio. It's a little trick done through the registry. You add a key, then a string with a list of all the placements of your guidelines. ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
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 required to effectively administer and manage Oracle...
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 technical details, Gmail likely implements measures...
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 synthesis of my design into a bitstream, not the C++...
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. Background colors can be used to highlight important...
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 has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.