Whitespace around single minus arithmetic, bug or intended behaviour?

125 views
Skip to first unread message

WoLpH

unread,
Jul 11, 2010, 9:08:45 AM7/11/10
to pep8
While testing some code I got the error "E225 missing whitespace
around operator" for a line like this:

if spam(eggs, -i): pass

In the examples I see these
Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: baz(**kwargs)
Okay: negative = -1
Okay: spam(-1)

Where the last 2 indicate that with a number it's allowed, but I
wonder if it's also allowed with a variable or not? That example seems
to be lacking.

So... is this behaviour intended and should I fix my code? Or should "-
i" be alllowed aswell?

~rick

Chris Clark

unread,
Jul 12, 2010, 1:57:12 PM7/12/10
to pe...@googlegroups.com

I wonder if the following is easier to grok?

if spam(eggs, -1 * i): pass

I'm not sure I have an opinion on this yet hence throwing this out there
for discussion. I suspect this is what you meant by "fix my code".

Chris

WoLpH

unread,
Jul 12, 2010, 2:27:14 PM7/12/10
to pep8
Than that's what I'll do :)

I have to say that I am wondering about the best solution too. With a
single character variable (i.e. "-i") it's fairly readable. However,
with a "-some_variable" it seems less readable and the "-1 *
some_variable" approach would be better.

~rick

Chris Beaven

unread,
Jul 12, 2010, 2:51:25 PM7/12/10
to pe...@googlegroups.com


On Tue, Jul 13, 2010 at 6:27 AM, WoLpH <Rick.va...@fawo.nl> wrote:

> > So... is this behaviour intended and should I fix my code? Or should "-
> > i" be alllowed aswell?
>
> I wonder if the following is easier to grok?
>
>     if spam(eggs, -1 * i): pass
>
> I'm not sure I have an opinion on this yet hence throwing this out there
> for discussion. I suspect this is what you meant by "fix my code".
>
> Chris

Than that's what I'll do :)

I have to say that I am wondering about the best solution too. With a
single character variable (i.e. "-i") it's fairly readable. However,
with a "-some_variable" it seems less readable and the "-1 *
some_variable" approach would be better.

~rick

Still, "-1 * i" isn't really a good PEP8 suggestion.

I'd consider the correct format for a negative variable to be "-i" rather than "- i". Having a space confuses the minus as an operator rather than a modifier. For that reason, I'd propose that E225 is laxed to allow modifiers. Or even a new check which ensures modifiers to have no space.
Reply all
Reply to author
Forward
0 new messages