Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to use __str__ and __repr__?

Reply
Thread Tools

how to use __str__ and __repr__?

 
 
Carl Banks
Guest
Posts: n/a
 
      06-08-2004
Carl Banks wrote:
>
>
> Jim Newton wrote:
>> hmm, even when i change it to calling str() rather than __str__()
>> it does not work.
>>
>> class another:
>> pass
>>
>> print another() # works
>> another().str() # does not work
>>
>> does anyone know why?

>
> str is a function, not a method.


(Ok, it's actually a type object.)


--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      06-08-2004
Jim Newton wrote:

> if that is the case then why does the following fail
>
> class another:
> pass
>
> another.__str__()
>
> I would think that would return a string such as
> "<__main__.another instance at 0x8132b64>"
> but it does not seem to.


Because __str__ is a special method which str defers to. If you want
the str of anObject, call str(anObject). The object will then show you
the string representation for that object. You shouldn't care whether
or not it's got that behavior from defining a __str__ method, or whether
it's gotten it from a parent class.

--
__ Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ To endure what is unendurable is true endurance.
-- (a Japanese proverb)
 
Reply With Quote
 
 
 
 
Jim Newton
Guest
Posts: n/a
 
      06-08-2004
wow that works...

def __repr__(self):
middle = " ".join( [ str(substr) for substr in self])
return "( " + middle + " )"

thanks
-jim


Carl Banks wrote:
> Carl Banks wrote:
>
>>
>>Jim Newton wrote:
>>
>>>hmm, even when i change it to calling str() rather than __str__()
>>>it does not work.
>>>
>>>class another:
>>> pass
>>>
>>>print another() # works
>>>another().str() # does not work
>>>
>>>does anyone know why?

>>
>>str is a function, not a method.

>
>
> (Ok, it's actually a type object.)
>
>


 
Reply With Quote
 
Jim Newton
Guest
Posts: n/a
 
      06-08-2004
wow that is great.

now the other question:

class Pair(list):
...

how can i "cast", "promote" [] to class Pair?




Erik Max Francis wrote:
> Jim Newton wrote:
>
>
>>if that is the case then why does the following fail
>>
>>class another:
>> pass
>>
>>another.__str__()
>>
>>I would think that would return a string such as
>>"<__main__.another instance at 0x8132b64>"
>>but it does not seem to.

>
>
> Because __str__ is a special method which str defers to. If you want
> the str of anObject, call str(anObject). The object will then show you
> the string representation for that object. You shouldn't care whether
> or not it's got that behavior from defining a __str__ method, or whether
> it's gotten it from a parent class.
>


 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      06-08-2004
Jim Newton wrote:

> wow that is great.
>
> now the other question:
>
> class Pair(list):
> ...
>
> how can i "cast", "promote" [] to class Pair?


You can't.

--
__ Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Sometimes there's no point in giving up.
-- Louis Wu
 
Reply With Quote
 
Peter Maas
Guest
Posts: n/a
 
      06-09-2004
Erik Max Francis schrieb:
> Jim Newton wrote:

[...]
>>class Pair(list):
>> ...
>>
>>how can i "cast", "promote" [] to class Pair?

>
>
> You can't.


Perhaps I've got it wrong but Jim probably asks for overwriting
the __getitem__ method.

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail (E-Mail Removed)
-------------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Problem with __str__ method and character encoding Chris Angelico Python 0 12-07-2012 02:33 PM
Re: Problem with __str__ method and character encoding peter Python 0 12-07-2012 02:22 PM
Short confusing example with unicode, print, and __str__ Gerard Brunick Python 0 03-05-2008 05:59 PM
Access to formatting controls from within __repr__ or __str__? Dan Sommers Python 3 02-19-2005 11:02 AM



Advertisments