python styles: why Use spaces around arithmetic operators?

Discussion in 'Python' started by Peng Yu, Jul 26, 2010.

  1. Peng Yu

    Peng Yu Guest

    This webpage http://www.python.org/dev/peps/pep-0008/ recommends the
    following. It looks to me that both styles are fine. Could anybody let
    me know what the rationale is behind this recommendation?

    - Use spaces around arithmetic operators:

    Yes:

    i = i + 1
    submitted += 1
    x = x * 2 - 1
    hypot2 = x * x + y * y
    c = (a + b) * (a - b)

    No:

    i=i+1
    submitted +=1
    x = x*2 - 1
    hypot2 = x*x + y*y
    c = (a+b) * (a-b)

    --
    Regards,
    Peng
    Peng Yu, Jul 26, 2010
    #1
    1. Advertising

  2. Peng Yu

    rantingrick Guest

    On Jul 26, 5:20 pm, Peng Yu <> wrote:
    > This webpagehttp://www.python.org/dev/peps/pep-0008/recommends the
    > following. It looks to me that both styles are fine. Could anybody let
    > me know what the rationale is behind this recommendation?


    The rational is simple. Guido is God and if you don't follow his words
    then you will be tortured. His favorite means is by forcing you to
    wear Dutch wooden shoes every day whist programming Ruby! ;-)
    rantingrick, Jul 27, 2010
    #2
    1. Advertising

  3. On 07/27/10 00:06, rantingrick wrote:
    > On Jul 26, 5:20 pm, Peng Yu<> wrote:
    >> This webpagehttp://www.python.org/dev/peps/pep-0008/recommends the
    >> following. It looks to me that both styles are fine. Could anybody let
    >> me know what the rationale is behind this recommendation?

    >
    > The rational is simple. Guido is God and if you don't follow his words
    > then you will be tortured. His favorite means is by forcing you to
    > wear Dutch wooden shoes every day whist programming Ruby! ;-)


    Wat is er mis met klompen?

    --
    mph
    Martin P. Hellwig, Jul 27, 2010
    #3
  4. Peng Yu

    rantingrick Guest

    > Martin wrote:
    >
    > Wat is er mis met klompen?


    Well specifically their made from wood and wood is a very hard
    substance. Also i did not go into detail but he makes sure to pick
    shoes that are three sizes too small. You know a good podiatrist can
    be tough to come by in these times. It's a pretty severe punishment if
    you ask me.
    rantingrick, Jul 27, 2010
    #4
  5. On Mon, 26 Jul 2010 17:20:09 -0500, Peng Yu wrote:

    > This webpage http://www.python.org/dev/peps/pep-0008/ recommends the
    > following. It looks to me that both styles are fine. Could anybody let
    > me know what the rationale is behind this recommendation?
    >
    > - Use spaces around arithmetic operators:


    Because it looks better and is easier to read. Operators are small
    (single characters) and sometimes need space around them to stand out.

    > i=i+1


    See? It's hideously ugly and cramped. It's much worse if you use larger
    names:

    sensiblynamedvariable=sensiblynamedvariable+1

    But use your common sense. I usually group powers, multiplications and
    divisions, and separate additions and subtractions:

    y = 2*x + 1 - (3*x - 4/(2 + x**2))**-2

    And unary + and - operators should always be grouped with their operand:

    y = -2 # not "- 2"



    --
    Steven
    Steven D'Aprano, Jul 27, 2010
    #5
  6. On Mon, Jul 26, 2010 at 4:31 PM, Steven D'Aprano
    <> wrote:
    > On Mon, 26 Jul 2010 17:20:09 -0500, Peng Yu wrote:
    >
    >> This webpage http://www.python.org/dev/peps/pep-0008/ recommends the
    >> following. It looks to me that both styles are fine. Could anybody let
    >> me know what the rationale is behind this recommendation?
    >>
    >>     - Use spaces around arithmetic operators:

    >
    > Because it looks better and is easier to read. Operators are small
    > (single characters) and sometimes need space around them to stand out.
    >
    >>           i=i+1

    >
    > See? It's hideously ugly and cramped. It's much worse if you use larger
    > names:
    >
    > sensiblynamedvariable=sensiblynamedvariable+1
    >
    > But use your common sense. I usually group powers, multiplications and
    > divisions, and separate additions and subtractions:
    >
    > y = 2*x + 1 - (3*x - 4/(2 + x**2))**-2
    >
    > And unary + and - operators should always be grouped with their operand:
    >
    > y = -2  # not "- 2"


    This is the rule that I use, with the exception that I will generally
    explicitly parenthesize the numerator in a division, since my eyes
    frequently gloss over the / symbol for some reason.

    Geremy Condra
    geremy condra, Jul 27, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,736
    Smokey Grindel
    Dec 2, 2006
  2. joshc
    Replies:
    5
    Views:
    539
    Keith Thompson
    Mar 31, 2005
  3. Amal
    Replies:
    1
    Views:
    1,733
    vipinlal
    Mar 4, 2010
  4. Stephen Hansen
    Replies:
    4
    Views:
    234
    Cousin Stanley
    Jul 31, 2010
  5. Sarah Allen
    Replies:
    14
    Views:
    199
    David A. Black
    Apr 15, 2010
Loading...

Share This Page