Python whitespace confusion

Python expects a whitespace before and after an arithmetic operator but complains about whitespace around =

Is this a general python rule or something which has been decided by Rhino developers?

Hmm, I’m not seeing that, no line too long or whitespace messages here. Is this ScriptEditor in Rhino or the Python script component in GH?

It’s in the Python script editor in Grasshopper.

(Just saying … I’m no more a Rhino user, I’m retired :blush: )

pycodestyle should be about style conventions only.
It shouldn’t prevent the code from running.

Still not seeing that…

Here’s a script in a Grasshopper file:

python_whitespace.gh (4.6 KB)

That’s what I see.

Line 8 raises no problem when changed to a = 1 + 1

Line 10 font_style = 0 however is fixed by removing the whitespaces.

I’ve been looking at the reference here: 2. Lexical analysis — Python 3.12.0 documentation

I only see this:

For me all of these work without a problem message

a=1 + 1
a=1+1
a = 1+1
a = 1 + 1

Is there some sort of ‘grammar’ checker that you turned on?

The rs.AddText() throws an error, but I don’t think that is the complaint here.

RhinoCodePluginGH.Legacy.NotSupportedInGHException: This type of object is not supported in Grasshopper, so this Python script cannot create it. You might want to use ‘scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc’ to use the Rhino doc, instead? If you do, remember to restore it: ‘scriptcontext.doc = ghdoc’.
at Rhino.Runtime.Code.Languages.PythonNet.CPythonCode.Execute(RunContext context)
at Rhino.Runtime.Code.Code.ExecTry(RunContext context, IPlatformDocument& doc, Object& docState)
at Rhino.Runtime.Code.Code.Run(RunContext context)

Hi,

you are doing two different things and its a flaw that Python devs decided to use the equals operator for argument assignment.

See:

font = “Arial” initializes a variable.

some_func(some_number=42) specifies an argument

now consider:
font = “Arial”
write_text(font=“Bahnschrift”)
print(font)

What do you expect? Do you see why formatting might matter. In C#
string font = “Arial”;
WriteText(font:”Bahnschrift”) ;

Therfore it is good practice to align to Pythons Style conventions. A static analyser helps you with that!

1 Like

FWIW those warnings are generated by a linter. I believe you can turn them off.

This might help:

Edit: On a side note, the error messages might be improved. In this case, by raising a syntax error exception and providing more useful information ala the current GHPython component and IDLE (paging doctor @eirannejad):

1 Like

Static Analysing/Linting is something good. A restrictive linter like you see in Pycharm is really good. Because if you work within a team, then there is no endless talk about code styling and metrics. Just take it, no discussion anymore about personal preferences. I had endless talks about it in the less restrictive world of C#. And its tiring and annoying when everybody has a different opinion on almost anything . People who made a linter are usually quite experienced and decided because of good reasons

I’m self taugth and I find I have difficulties understanding what’s wrong or right. In this case I was mostly just confused that in one situation a space is expected but in another situation it raises an error? I think I’ll just put a whitespace before and after = or any operator.

The yellow triangle things are linting warnings, not errors. You can ignore them, the code will run just fine.

As pointed out by @AndersDeleuran , line 8 is invalid syntax. Don’t mix the two issues (linting warnings vs code errors), don’t get confused by them.

I would turn off the linter (diagnostics, also pointed out by @AndersDeleuran here Python whitespace confusion - #10 by AndersDeleuran ), and concentrate on learning the language first. Once understand well enough you can turn on linting, which helps you to adhere to some pythonic conventions.

3 Likes

I would disagree to disable a static analyser. Its a feature and not just about styling, it can also help you in preventing certain beginner pitfalls and even prevent you from not so obvious bugs.

Most of us are self-taught. This makes it even more important to agree on a minimum set of rules.

You can but you should not ignore warnings. At least you should only ignore a warning if you know what its about.

Lets imagine you are writing code, and you decide to yield a warning. This usually means something odd was detected, but if you know what you are doing, then ignore it. But obviously you don’t understand it yet