Say what?!

Both of these python nodes have the same input…

Spot the difference.

I was having issues wondering why my code wasn’t outputting like the one on the right…

I put it into ChatGPT and got the code on the right, which works.

I have just worked out what the difference is.

My code on the left is using a tab on line 26.
ChatGPT’s code is using 4 spaces on line 26.

Why on earth does this make a difference?

I’m in Revit 21 with Dyno 2.6.1.

If I understand the situation well, what you’re stumbling over here is a Python Syntax problem. For Python, indentation matters as you know, but it also matters how you define the indentations. It can bei EITHER all tabs OR all spaces. Mixing them is not allowed.

What happens then is that Dynamo executes your script up to the point where you try to append the list you created to another list in line 26. At that line, Python throws an error, and script execution stops. But before that, you have already filled in some data in the loop, and that’s what you see in the output.

3 Likes

Demoing this in Eclipse, the IDE warns you about inconsistent indentations or detents (I usually use tabs, on the last line I used spaces on purpose):

mixing_indent

1 Like

Ahh! I was super confused because I always use tab…

I think I must have put it into ChatGPT before and cut and pasted without thinking… So it must be spaces now and I didn’t realise!

2 Likes

I like using the “show white space in python editor” feature to quickly identify these issues, as the CPython 3 and IronPython 2.7 engines have different settings.

image

2 Likes

The dangers of innernetz code copy-paste :stuck_out_tongue: Could try asking ChatGPT to always use 4x spaces instead of tabs too!

2 Likes

Tabs are better. :stuck_out_tongue:

4 Likes

You mean ask it to use tabs not spaces.

Tabs > spaces nods

+1 to Jacob :grin:

Jokes aside, why would you use spaces? Mashing my keyboard 4x more can’t be good for the keyboard or my hands?

2 Likes

Primarily as it’s convention in the world of Python, so less likely to run into mixed indentation issues when copying code from the internet :slight_smile:

1 Like

It’s a debate that has existed for a long time. One of the reasons to be “pro” spaces is that it is consistent on every machine and every reader. IDEs can have different settings for what a “tab” press does, like in Visual Studios, you can set a tab to be either a tab or a set amount of spaces. These are settings per user.

If one person has tab to be 4 spaces, one has it to be a tab, and another has it to be 2 spaces, when they collaborate everything breaks.

Along with that, some readers (like IDE vs notepad, etc.) might display tabs differently. If it is commented a certain way but the tabs don’t line up when the next person opens it, it just gets hard to read and confusing. Spaces are uniform (outside of like MS Word if for some reason you are using that).

3 Likes

Define ‘consistent’… :wink:

I’ve worked with a few people who are visually impaired, and their braille screen reader gave 40 characters at a time. Four spaces eats up 1/10 of the readable real estate. That’s one offset, but I am guessing you often have two levels in quite a few sets of code. Well that’s 1/5 of what an impaired user can see at a time.

If you used tabs it would be 1/20th.

Now that is a very unique situation, but I want what I do with Dynamo to be as accessible and inclusive as possible; if using tabs makes some PEP advocates angry but allows one hypothetical user to utilize the tool more readily… well in that case PEP is going out the window.

The disadvantaged user gets priority. Every. Single. Time.

2 Likes

You could argue the same for visually impaired.

By consistent I mean that every program has spaces be exactly 1 character, so 4 spaces is 4 characters. Doesn’t matter what program you use to read, spaces are treated consistently. Tabs can mean different things in different programs, most are 4 characters, but notepad (bad example but still counts) have tabs as 8 characters.

What you wrote out to look a certain way in your IDE isn’t the same for everyone who has to use it.

But I am in the “tab set to 4 spaces” category, so I can press tab but get the consistency of spaces.

Edit: Also an option for screen readers, Google’s style guide has their indents set to only 2 spaces, so its not as much real estate but still has the consistency of spaces.

1 Like

It’s one character which is 8 units long. Turning on the ‘display white space’ in the editor being used will help clear that up. In either case, the Python interpreter won’t care how anything is displayed as long as the number of tabs (or spaces) is consistent. If it shows as 8 characters, 3 characters, or 200 characters wide is irrelevant. Similarly I could change the display thickness between new line characters in a given IDE, but it’s a moot point as far as the interpreter is concerned - each line terminates with \n under the hood.

Either way I am in team tabs for Python work; I think everyone can agree that allowing both is good, bir this is not possible in the current CPython implementation as everything is four spaces, which is a known bug that goes back as far as the CPython engine’s history.

1 Like

This is a basic python issue that is often hard to detect.

I use NotePad++ to code, there you can

  • visualise tabs and spaces
  • replace all tabs with spaces
  • set it up to make spaces if you press the tab key

(Maybe a good feature for the dynamo python editor?)

This issue is very dangerous, because you often get very weird results and dont know whats going on.

Watch your indentions! :grinning:

Dynamo has had Show Whitespace Characters for a long time now :blush: In later versions it’s under the Preferences Panel, in earlier versions under the Settings menu.

Newer Dynamo versions:

Older Dynamo versions:
image

1 Like

“It’s always been done that way”…

Not really a good reason imo. :neutral_face: