473,659 Members | 3,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eval(repr(objec t)) 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(objec t()))
------------------------------------------------------------
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 3999

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(objec t()))
------------------------------------------------------------
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(objec t()))
------------------------------------------------------------
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************ *********@i42g2 000cwa.googlegr oups.com>,
"Hardcoded Software" <ha************ ****@gmail.comw rote:
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(objec t()))
------------------------------------------------------------
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.washingt on.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(sorte d(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+1 79-']]*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(objec t()))
------------------------------------------------------------
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
2235
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 phpinfo(), I get information that makes it seem like PHP is running as root. Isn't this a security problem? This is some of the info I'm getting back from phpinfo():
23
2480
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 thread? -- Antoon Pardon
2
2686
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 advance!
17
2411
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 replacement" and would generally like 5/2 ==2.5 So, I was contemplating to default all my modules/scripts to start with "from __future__ import division" but if it is never coming (in this decade, that is) then it would be a
6
1536
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 works well void copy (char *s, char *t) { int i = 0;
9
7026
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 another ASP page. The other page retrieves the value with Request.Form('album'); On Firefox this works fine every time. On IE6, I always get nothing. I'm pretty sure it's the posting side that is at fault, so that's what I've shown here. Oh, I tried...
18
2159
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 has a <theadand a <tbody>. In the first example, the <theadstays fixed at the top of the div as it scrolls. In the second example the <theadscrolls away. Could anyone tell me what about the second page makes it not work? Thanks Luminari
1
3010
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
1433
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. http://files.me.com/tlhintoq/lop8xc Here's the weird part... take a close look at line 8. The lines with the class header don't line up on the 5th character position. This is a monospaced font, yet this line is offset by PART of a character...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.