Well, like I said, it's all about the REPL. __str__ is ignored here. Thus:
class Foo:
def __str__(self):
return 'A'
def __str__(self):
return 'A'
>>> f = Foo()
>>> f
<__main__.Foo instance at 0x009DA1C0>
In my own code I like to return something like 'Foo(a1=v1, a2=v2)' which with an appropriate constructor follows the guideline that __repr__ should be something you can turn back into the object instance. Something like
L = ['%s=%s' % (a, repr(getattr(self, a))) for a in dir(self) if not callable(getattr(self, a)) and not a.startswith('_')]
return '%s(%s)' % (self.__class__.__name__, ','.join(L))
On 12/30/05, Michael Bayer <[EMAIL PROTECTED]
> wrote:
On Dec 29, 2005, at 3:25 PM, Jonathan Ellis wrote:
> Nothing I ran across has __repr__ defined which makes REPL a pita,
> having to dir everything...
what are you looking for with regards to __repr__ ? Most SQL
construction objects define __str__ which gives you their SQL
representation, and I usually rely on __repr__ to tell me what actual
objects I am dealing with. Are you just looking for SQL construction
objects to display generated SQL via __repr__ ? or is there
something in the ORM youre looking for ?
--
Jonathan Ellis
http://spyced.blogspot.com

