equivelant of Java's toString() method? (not repr)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Hunsley

    #1

    equivelant of Java's toString() method? (not repr)

    Pretty simple seeming question, but can't find answer via google or docs...

    I am using urllib2 as follows:

    handle = urlopen(req, postdata) # and URL to return a handle
    on
    ...
    print handle.info()

    the print statement prints out the headers:

    Content-Type: text/html;charset=IS O-8859-1
    Content-Length: 2845
    Date: Tue, 01 Feb 2005 12:40:28 GMT
    Server: Apache Coyote/1.0
    Connection: close

    which is the string I want to use.
    However, if I write a function call using this:

    log(handle.info ())

    I get this:

    TypeError: cannot concatenate 'str' and 'instance' objects

    (The log function expects a string)
    What is the magic incantation to get the string I want so I can pass it
    to my function?
    I've tried repr() but that isn't it (I suspected it wouldn't be).
    I suppose I'm looking for the equivelant of Java's toString() method...

    thanks!
    alex




  • Alex Hunsley

    #2
    Re: equivelant of Java's toString() method? (not repr)

    Alex Hunsley wrote:[color=blue]
    > Pretty simple seeming question, but can't find answer via google or docs...
    >
    > I am using urllib2 as follows:
    >
    > handle = urlopen(req, postdata) # and URL to return a handle on
    > ...
    > print handle.info()
    >
    > the print statement prints out the headers:
    >
    > Content-Type: text/html;charset=IS O-8859-1
    > Content-Length: 2845
    > Date: Tue, 01 Feb 2005 12:40:28 GMT
    > Server: Apache Coyote/1.0
    > Connection: close
    >
    > which is the string I want to use.
    > However, if I write a function call using this:
    >
    > log(handle.info ())
    >
    > I get this:
    >
    > TypeError: cannot concatenate 'str' and 'instance' objects
    >
    > (The log function expects a string)
    > What is the magic incantation to get the string I want so I can pass it
    > to my function?
    > I've tried repr() but that isn't it (I suspected it wouldn't be).
    > I suppose I'm looking for the equivelant of Java's toString() method...
    >
    > thanks!
    > alex[/color]

    Replying to meself...
    D'oh! Just found the str() function which is just what I need!

    log(str(handle. info())) works fine....

    thx
    alex

    Comment

    • Roy Smith

      #3
      Re: equivelant of Java's toString() method? (not repr)

      Alex Hunsley <lard@tardis.ed .ac.molar.uk> wrote:[color=blue]
      > I suppose I'm looking for the equivelant of Java's toString() method...[/color]

      That would be str(), which for the most part, just calls your object's
      __str__() method. If your object doesn't have an __str__() method, there's
      nothing magic Python can do to invent one.

      The difference between repr() and str() is that repr() is supposed to
      produce a parsable representation of the object; one which could be parsed
      by a Python interpreter to re-generate the original object. On the other
      hand, str() is supposed to produce a human-readable string.

      Comment

      • Ola Natvig

        #4
        Re: equivelant of Java's toString() method? (not repr)

        Alex Hunsley wrote:[color=blue]
        > Pretty simple seeming question, but can't find answer via google or docs...
        >
        > I am using urllib2 as follows:
        >
        > handle = urlopen(req, postdata) # and URL to return a handle on
        > ...
        > print handle.info()
        >
        > the print statement prints out the headers:
        >
        > Content-Type: text/html;charset=IS O-8859-1
        > Content-Length: 2845
        > Date: Tue, 01 Feb 2005 12:40:28 GMT
        > Server: Apache Coyote/1.0
        > Connection: close
        >
        > which is the string I want to use.
        > However, if I write a function call using this:
        >
        > log(handle.info ())
        >
        > I get this:
        >
        > TypeError: cannot concatenate 'str' and 'instance' objects
        >
        > (The log function expects a string)
        > What is the magic incantation to get the string I want so I can pass it
        > to my function?
        > I've tried repr() but that isn't it (I suspected it wouldn't be).
        > I suppose I'm looking for the equivelant of Java's toString() method...
        >
        > thanks!
        > alex
        >
        >
        >
        >[/color]

        use:

        log(str(handle. info()))

        str creates a string object from a supplied object with a __str__
        method. I think print calls str on what it is going to print



        --
        --------------------------------------
        Ola Natvig <ola.natvig@inf osense.no>
        infoSense AS / development

        Comment

        Working...