My first Python program

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Thu Oct 14 22:35:24 CEST 2010


Seebs writes:

>> For long strings, another option is triple-quoting as you've seen in doc
>> strings: print """foo
>> bar""".
>
> I assume that this inserts a newline, though, and in this case I don't
> want that.

True.
    $ python
    >>> """foo  
    ... bar"""
    'foo\nbar'
    >>> """foo\
    ... bar"""
    'foobar'
    >>> "foo\ 
    ... bar"
    'foobar'
    >>> "foo"  "bar"
    'foobar'

>> SourceFile.__repr__() looks like it should be a __str__().  I haven't
>> looked at how you use it though.  But __repr__ is supposed to
>> look like a Python expression to create the instance: repr([2]) = '[2]',
>> or a generic '<foo instance>': repr(id) = '<built-in function id>'.
>
> Ahh!  I didn't realize that.  I was using repr as the "expand on it enough
> that you can see what it is" form -- more for debugging than for
> something parsable.

No big deal, then, except in the "idiomatic Python" sense.
__str__ for the informal(?) string representation of the object,
  def __repr__(self):
      return "<%s object with %s>" % (self.__class__.__name__, <foo>)
and you have a generic 2nd case, but looks like it'll be unusually long
in this case, or just define some ordinary member name like info() or
debug().

-- 
Hallvard



More information about the Python-list mailing list