Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

eval(repr(object)) hardly ever works

瀏覽次數:10 次
跳到第一則未讀訊息

Matthew Wilson

未讀,
2006年9月13日 上午10:10:402006/9/13
收件者:

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/wilsonwiki/SasAndMakefiles

Hardcoded Software

未讀,
2006年9月13日 上午10:17:252006/9/13
收件者:

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')

sk...@pobox.com

未讀,
2006年9月13日 上午10:29:472006/9/13
收件者:Matthew Wilson、pytho...@python.org

Matthew> I understand that idea of an object's __repr__ method is to
Matthew> return a string representation that can then be eval()'d back
Matthew> to 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.

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

Matthew> Any thoughts?

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

Skip

Steve Holden

未讀,
2006年9月13日 上午10:38:032006/9/13
收件者:pytho...@python.org
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

Matthew Wilson

未讀,
2006年9月13日 上午11:02:572006/9/13
收件者:
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.

Fredrik Lundh

未讀,
2006年9月13日 上午11:23:232006/9/13
收件者:pytho...@python.org
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>

Donn Cave

未讀,
2006年9月13日 中午12:19:532006/9/13
收件者:
In article <1158157045....@i42g2000cwa.googlegroups.com>,

"Hardcoded Software" <hardcoded...@gmail.com> wrote:
> 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

sjde...@yahoo.com

未讀,
2006年9月13日 下午3:43:022006/9/13
收件者:
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

Tal Einat

未讀,
2006年9月16日 晚上10:13:582006/9/16
收件者:

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]

Dustan

未讀,
2006年9月17日 上午9:38:592006/9/17
收件者:

You have to write the code to return a proper representation.

0 則新訊息