Generator expression parenthesis mixed with function call ones

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

    Generator expression parenthesis mixed with function call ones

    [Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
    on win32]

    Given the following:
    >>sum(i for i in range(10))
    45
    >>def f(*args) : print args
    ....
    >>f(i for i in range(10))
    (<generator object at 0x00A79788>,)
    >>def f(a,*args) : print a,ar
    ....
    >>f(4,i for i in range(10))
    File "<stdin>", line 1
    SyntaxError: invalid syntax


    Why does Python allow generator expression parenthesis to be mixed with
    function call parenthesis when there is only one parameter ?

    IMHO this should be forbidden, usage must not be different when there is
    only one parameter and when there are more parameters.
    User should all times explicitly use () for a generator expression and
    [] for a list comprehension expression.


  • Facundo Batista

    #2
    Re: Generator expression parenthesis mixed with function call ones

    Laurent Pointal wrote:

    >>>f(4,i for i in range(10))
    File "<stdin>", line 1
    SyntaxError: invalid syntax
    >
    >
    Why does Python allow generator expression parenthesis to be mixed with
    function call parenthesis when there is only one parameter ?
    For simplicity and elegant coding, so you can do something like you did
    at first:

    sum(i for i in range(10))

    IMHO this should be forbidden, usage must not be different when there is
    only one parameter and when there are more parameters.
    The problem in your last test is that if you use more than one argument,
    you *must* use the parenthesis. In Py2.5 there's a better message error:
    >>f(4,i for i in range(10))
    File "<stdin>", line 1
    SyntaxError: Generator expression must be parenthesized if not sole argument


    The correct way to do that is:
    >>f(4,(i for i in range(10)))
    4 (<generator object at 0xb7dab56c>,)

    Regards,

    --
    .. Facundo
    ..
    Blog: http://www.taniquetil.com.ar/plog/
    PyAr: http://www.python.org/ar/


    Comment

    • Laurent Pointal

      #3
      Re: Generator expression parenthesis mixed with function call ones

      Facundo Batista a écrit :
      Laurent Pointal wrote:
      >
      >
      >>>>f(4,i for i in range(10))
      > File "<stdin>", line 1
      >SyntaxError: invalid syntax
      >>
      >>
      >Why does Python allow generator expression parenthesis to be mixed with
      >function call parenthesis when there is only one parameter ?
      >
      For simplicity and elegant coding, so you can do something like you did
      at first:
      >
      sum(i for i in range(10))
      How a Python beginner know that he is using a generator and not a
      list-comprehension ?
      >IMHO this should be forbidden, usage must not be different when there is
      >only one parameter and when there are more parameters.
      >
      The problem in your last test is that if you use more than one argument,
      you *must* use the parenthesis. In Py2.5 there's a better message error:
      >
      >>>f(4,i for i in range(10))
      File "<stdin>", line 1
      SyntaxError: Generator expression must be parenthesized if not sole argument
      >
      >
      The correct way to do that is:
      >
      >>>f(4,(i for i in range(10)))
      4 (<generator object at 0xb7dab56c>,)
      Thanks, I know. My example is just to show a point I consider to be
      non-coherent (different processing if there is one argument or more than
      one).

      A+

      Laurent.

      Comment

      • Gabriel Genellina

        #4
        Re: Generator expression parenthesis mixed with function call ones

        En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
        <laurent.pointa l@limsi.frescri bió:
        >>>f(4,i for i in range(10))
        File "<stdin>", line 1
        SyntaxError: invalid syntax
        2.5 has a better error message:
        pyf(4,i for i in range(10))
        File "<stdin>", line 1
        SyntaxError: Generator expression must be parenthesized if not sole
        argument
        Why does Python allow generator expression parenthesis to be mixed with
        function call parenthesis when there is only one parameter ?
        Because they are redundant when only one argument is used.
        sum(i for i in range(10)) looks better than sum((i for i in range(10)))
        "Beautiful is better than ugly", and "Readabilit y counts."
        IMHO this should be forbidden, usage must not be different when there is
        only one parameter and when there are more parameters.
        It's similar to "%d" % 123 vs. "%d" % (123,)
        """Special cases aren't special enough to break the rules.
        Although practicality beats purity."""
        User should all times explicitly use () for a generator expression and
        [] for a list comprehension expression.
        For a list comprehension, yes. For a generator, not always.

        --
        Gabriel Genellina

        Comment

        • Laurent Pointal

          #5
          Re: Generator expression parenthesis mixed with function call ones

          Dennis Lee Bieber wrote:
          On Wed, 07 Mar 2007 17:15:33 +0100, Laurent Pointal
          <laurent.pointa l@limsi.frdecla imed the following in comp.lang.pytho n:
          >
          >>
          >How a Python beginner know that he is using a generator and not a
          >list-comprehension ?
          >>
          A list comprehension ALWAYS has list brackets [...] around it...
          Yes, and a generator expression ALWAYS has round brackets... which can be
          confused with function call ones when it is used in a single argument
          function call...
          I still personnaly think function call round brackets and generator
          expression round brackets should both be present.

          Comment

          • Laurent Pointal

            #6
            Re: Generator expression parenthesis mixed with function call ones

            Gabriel Genellina wrote:
            En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
            <laurent.pointa l@limsi.frescri bió:
            >
            >>>>f(4,i for i in range(10))
            > File "<stdin>", line 1
            >SyntaxError: invalid syntax
            >
            2.5 has a better error message:
            pyf(4,i for i in range(10))
            File "<stdin>", line 1
            SyntaxError: Generator expression must be parenthesized if not sole
            argument
            >
            >Why does Python allow generator expression parenthesis to be mixed with
            >function call parenthesis when there is only one parameter ?
            >
            Because they are redundant when only one argument is used.
            sum(i for i in range(10)) looks better than sum((i for i in range(10)))
            "Beautiful is better than ugly", and "Readabilit y counts."
            I complement my reply.

            Beginners generally know about list-comprehensions and associate the
            syntax "x for x in asequence" to a list expression. I'm not sure that
            reading a "f(i for i in range(20))" they understand that they are dealing
            with a different object kind.

            If function f start by a if len(myparameter )...
            TypeError: len() of unsized object

            If function f goes among its parameter with "for x in myparameter" more than
            once, other loops goes throught an empty loop.
            >IMHO this should be forbidden, usage must not be different when there is
            >only one parameter and when there are more parameters.
            >
            It's similar to "%d" % 123 vs. "%d" % (123,)
            """Special cases aren't special enough to break the rules.
            Although practicality beats purity."""
            In that case there cannot be confusion.

            A+

            Laurent.

            Comment

            • Gabriel Genellina

              #7
              Re: Generator expression parenthesis mixed with function call ones

              En Wed, 07 Mar 2007 15:21:57 -0300, Laurent Pointal
              <laurent.pointa l@wanadoo.fresc ribió:
              Gabriel Genellina wrote:
              >
              >En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
              ><laurent.point al@limsi.frescr ibió:
              >>
              >>Why does Python allow generator expression parenthesis to be mixed with
              >>function call parenthesis when there is only one parameter ?
              >Beginners generally know about list-comprehensions and associate the
              syntax "x for x in asequence" to a list expression. I'm not sure that
              But list comprehensions have [] around... When you don't see the [], you
              should think "this is something different"...
              reading a "f(i for i in range(20))" they understand that they are dealing
              with a different object kind.
              Exactly.
              If function f start by a if len(myparameter )...
              TypeError: len() of unsized object
              >
              If function f goes among its parameter with "for x in myparameter" more
              than
              once, other loops goes throught an empty loop.
              Then he thinks "Something is wrong!", and reads some Python books, or asks
              here, and somebody will point him to the Python Tutorial, section 9.11:

              >>IMHO this should be forbidden, usage must not be different when there
              >>is only one parameter and when there are more parameters.
              If you want to review the original discussion on how to spell a generator
              expression (called "accumulato r display" by that time) see


              --
              Gabriel Genellina

              Comment

              • Laurent Pointal

                #8
                Re: Generator expression parenthesis mixed with function call ones

                Gabriel Genellina a écrit :
                If you want to review the original discussion on how to spell a
                generator expression (called "accumulato r display" by that time) see
                http://mail.python.org/pipermail/pyt...er/038868.html
                That's a long discussion... and it seem I'm not alone to dislike parens
                confusion.



                Thanks for the pointer.

                Laurent.

                Comment

                Working...