Python Forum
It seems like the overall Python syntax would be error prone
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
It seems like the overall Python syntax would be error prone
#1
I just started reading an intro to Python piece. It shows simple examples of several statements (def, for, while). I'm just getting started, but it looks like the Python compiler (interpreter?) depends on the indentation of the statements to define what is or is not part of a procedure. It looks like there are no End statements.

Is this not error prone? Does it matter how much a statement is indented?

It seems odd to me that spaces have a role in syntax other than in separating tokens.

I would appreciate someone helping me understand why

for i in range(10):
   statement
   statement
   statement
Is as good (clear, readable) as something like

for i in range(10):
   statement
   statement
   statement
end i
And, of course, it gets worse with nested structures

Thanks
Reply
#2
(Apr-01-2020, 08:04 PM)CynthiaMoore Wrote: ...it looks like the Python compiler (interpreter?) depends on the indentation of the statements to define what is or is not part of a procedure.

Correct.

Quote:Is this not error prone?
Errors pop up in any language. But no, there doesn't seem to be any reason to suspect that this style is more error prone, and probably reduces the likelihood of certain types of errors. See the link below for some rationale on why that may be.

Quote:Does it matter how much a statement is indented?

The interpreter requires only that the spacing is consistent. But for readability by others, there are standards that should be followed unless there is a good reason to deviate. 4 spaces per level is suggested. https://www.python.org/dev/peps/pep-0008/#indentation

Quote:It seems odd to me that spaces have a role in syntax other than in separating tokens.

I would appreciate someone helping me understand why

For some of the background, you can see the history FAQ entries in the python documentation.
https://docs.python.org/3/faq/design.htm...statements
CynthiaMoore likes this post
Reply
#3
It bothered me for about 2 weeks. In some ways it is easier to spot errors in python because everything lines up.

Wait til you start working with classes. The lasses-faire way of setting attributes in python classes drove me nuts for 3 weeks. What do you mean ClassA does not have attribute date? I set date right there in line 21! Then you realize your program has not yet reached line 21 and ClassA.date does not exist yet. Or the really crazy thing that instances a and b, both of ClassA, can have different attributes!

After working with it for a while you'll stop missing your enclosing curly brackets and ending semicolons or whatever syntactic sugar you are used to using and see how some of the things you thought were really sloppy in python end up providing power and flexibility that other languages cannot match.
CynthiaMoore likes this post
Reply
#4
It is a design aesthetic(thank god for spellcheck). Whether you like it or not is completely subjective. As a fossil whose first language was ATARI BASIC and then fortran, I like it a lot more than I hate it.
Reply
#5
One main benefit of Python's syntax: it forces coders to properly indent their code. Even languages with brackets, semicolons, and other delimiters for statements and code blocks recommend indentation. It makes code easier to read because everything at the same, uninterrupted indentation level is part of the same code block.

The amount of indentation does matter relative to surrounding statements. Anything indented more than preceding line is understood to be a code block under the previous line, such as with if, def, and for statements, etc. Anything indented less than a preceding line is not part of that same code block.

As a result of that dynamic, an end keyword or other terminating statement that you'd see in Ruby, VBA, etc. is not needed. The instant the indentation of a line differs from the preceding lines, it's the beginning of a new code block.

For new Pythonistas and students, this does cause consternation. Indentation errors occur and the interpreter often catches them and raises an error.

Nested structures can be troublesome in any language. Once you hit that sixth nested code block, brackets don't help much. As a maxim (and part of the Zen of Python), flat code is better than nested code. I try to keep nesting down to one level as much as possible.
CynthiaMoore likes this post
Reply
#6
My only dislike, really, is that sometimes the code is too vertical (i.e. laddered) if you adopt the most consistent coding style.
CynthiaMoore likes this post
Reply
#7
Wow! WOW!!! Smile Big Grin LOL

This is a really active forum. Thanks for all the really helpful comments -- much appreciated.
Reply
#8
CynthiaMoore Wrote:Is as good (clear, readable) as something like

for i in range(10):
   statement
   statement
   statement
end i
We are cooler than Ruby people and don't need a end to understand Hand
# Ruby
nums = [1, 2, 3, 4, 5]
for i in nums do
  puts i
end 
# Python
nums = [1, 2, 3, 4, 5]
for i in nums:
    print(i)
CynthiaMoore likes this post
Reply
#9
(Apr-01-2020, 08:29 PM)stullis Wrote: One main benefit of Python's syntax: it forces coders to properly indent their code.
I am 100% behind proper indentation and all other readability measures, but I'll be interested to see how many times I get a maddeningly elusive bug because I realized that a line of code in one level of nesting belonged in a different level and didn't quite get the indentations lined up correctly.

I guess if I were designing a language today, I'd look at including the End statements and then having the IDE enforce the indentation. With modern graphic tools, the IDE could also do something with color or font or highlighting or something. It's very different from when I used to type BAL statements onto punch cards on an 029. Tongue
Reply
#10
(Apr-02-2020, 12:12 AM)CynthiaMoore Wrote: It's very different from when I used to type BAL statements onto punch cards on an 029. Tongue
Hahaha! You are more fossilized than I am! Tongue
CynthiaMoore likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020