Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python styles: why Use spaces around arithmetic operators?

Reply
Thread Tools

python styles: why Use spaces around arithmetic operators?

 
 
Peng Yu
Guest
Posts: n/a
 
      07-26-2010
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
 
Reply With Quote
 
 
 
 
rantingrick
Guest
Posts: n/a
 
      07-26-2010
On Jul 26, 5:20*pm, Peng Yu <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
 
Martin P. Hellwig
Guest
Posts: n/a
 
      07-26-2010
On 07/27/10 00:06, rantingrick wrote:
> On Jul 26, 5:20 pm, Peng Yu<(E-Mail Removed)> 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

 
Reply With Quote
 
rantingrick
Guest
Posts: n/a
 
      07-26-2010
> 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.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-26-2010
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
 
Reply With Quote
 
geremy condra
Guest
Posts: n/a
 
      07-27-2010
On Mon, Jul 26, 2010 at 4:31 PM, Steven D'Aprano
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: python styles: why Use spaces around arithmetic operators? Stephen Hansen Python 4 07-31-2010 01:00 AM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM



Advertisments