Cast list of objects to list of strings

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

    Cast list of objects to list of strings

    I have this function:

    def write_err(obj):
    from sys import stderr
    stderr.write(st r(obj)+"\n")

    and I'd like to rewrite it to take a variable number of objects.
    Something like this:

    def write_err(*objs ):
    from sys import stderr
    stderr.write(" ".join(objs)+"\ n")

    but I lose the property that the function works on any object. What's
    the simplest way to fix this? In essence, I need to cast a list of
    objects to a list of strings. I'd like to do just "str(objs)" but that
    (obviously) doesn't quite do what I need.

  • Larry Bates

    #2
    Re: Cast list of objects to list of strings

    bukzor wrote:
    I have this function:
    >
    def write_err(obj):
    from sys import stderr
    stderr.write(st r(obj)+"\n")
    >
    and I'd like to rewrite it to take a variable number of objects.
    Something like this:
    >
    def write_err(*objs ):
    from sys import stderr
    stderr.write(" ".join(objs)+"\ n")
    >
    but I lose the property that the function works on any object. What's
    the simplest way to fix this? In essence, I need to cast a list of
    objects to a list of strings. I'd like to do just "str(objs)" but that
    (obviously) doesn't quite do what I need.
    >
    I think what you want is:

    def write_err(*args ):
    from sys import stderr
    stderr.write("\ n".join([str(o) for o in args]))


    but then I don't really understand why you would want such a function so I could
    be way wrong.

    -Larry

    Comment

    • jay graves

      #3
      Re: Cast list of objects to list of strings

      On Jun 2, 4:02 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
      I think what you want is:
      def write_err(*args ):
      from sys import stderr
      stderr.write("\ n".join([str(o) for o in args]))
      Slight nitpick. If you are using version >= 2.4 you could use a
      generator expression instead of a list comprehension to avoid building
      and throwing away a list.

      "\n".join(str(o ) for o in args)

      This PEP introduces generator expressions as a high performance, memory efficient generalization of list comprehensions PEP 202 and generators PEP 255.


      Very unlikely to yield a material time difference in this case but
      cleaner IMO.

      ....
      Jay Graves

      Comment

      • Gary Herron

        #4
        Re: Cast list of objects to list of strings

        bukzor wrote:
        I have this function:
        >
        def write_err(obj):
        from sys import stderr
        stderr.write(st r(obj)+"\n")
        >
        and I'd like to rewrite it to take a variable number of objects.
        Something like this:
        >
        def write_err(*objs ):
        from sys import stderr
        stderr.write(" ".join(objs)+"\ n")
        >
        but I lose the property that the function works on any object.
        No you don't. If you were happy with printing the str(...) of a single
        objects, why not just printout the (concatenation) of the str(...) of
        each of many objects?

        stderr.write(" ".join([str(b) for b in objs])+"\n")

        Gary Herron



        What's
        the simplest way to fix this? In essence, I need to cast a list of
        objects to a list of strings. I'd like to do just "str(objs)" but that
        (obviously) doesn't quite do what I need.
        >
        --

        >

        Comment

        • bukzor

          #5
          Re: Cast list of objects to list of strings

          On Jun 2, 2:56 pm, jay graves <jaywgra...@gma il.comwrote:
          On Jun 2, 4:02 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
          >
          I think what you want is:
          def write_err(*args ):
          from sys import stderr
          stderr.write("\ n".join([str(o) for o in args]))
          >
          Slight nitpick. If you are using version >= 2.4 you could use a
          generator expression instead of a list comprehension to avoid building
          and throwing away a list.
          >
          "\n".join(str(o ) for o in args)
          >
          This PEP introduces generator expressions as a high performance, memory efficient generalization of list comprehensions PEP 202 and generators PEP 255.

          >
          Very unlikely to yield a material time difference in this case but
          cleaner IMO.
          >
          ...
          Jay Graves
          Thanks! That's exactly what I was looking for.

          Comment

          • Gabriel Genellina

            #6
            Re: Cast list of objects to list of strings

            En Mon, 02 Jun 2008 18:56:08 -0300, jay graves <jaywgraves@gma il.com>
            escribió:
            On Jun 2, 4:02 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
            >I think what you want is:
            >def write_err(*args ):
            > from sys import stderr
            > stderr.write("\ n".join([str(o) for o in args]))
            >
            Slight nitpick. If you are using version >= 2.4 you could use a
            generator expression instead of a list comprehension to avoid building
            and throwing away a list.
            >
            "\n".join(str(o ) for o in args)
            >
            This PEP introduces generator expressions as a high performance, memory efficient generalization of list comprehensions PEP 202 and generators PEP 255.

            >
            Very unlikely to yield a material time difference in this case but
            cleaner IMO.
            Still nitpicking: using a generator expression in this case has no
            advantage. The first thing that str.join does is to create a list out of
            its argument (unless it is already a list or a tuple). In fact, a list
            comprehension is faster here.

            --
            Gabriel Genellina

            Comment

            • jay graves

              #7
              Re: Cast list of objects to list of strings

              On Jun 2, 8:36 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
              Still nitpicking: using a generator expression in this case has no
              advantage. The first thing that str.join does is to create a list out of
              its argument (unless it is already a list or a tuple). In fact, a list
              comprehension is faster here.
              Really! I stand corrected.

              I'm not a C programmer but I peeked at the source. I see that it
              makes a pass to calculate the total size and then another pass to
              catenate the items, which makes sense from a memory allocation
              standpoint.

              To compare and contrast, I looked up the 'sum' built in and saw that
              it used the iterator protocol, (PyObject_GetIt er). I assume that the
              other similar functions (min,max, etc) would do the same.

              Thanks for setting me straight.
              ....
              Jay

              Comment

              Working...