-1

I am trying to figure out why some simple changes can mess up a program in any programming language. It can get annoying and make you rage.

Here is an example of a working code:

class Person():
    def __init__(self):
        print("New Person")

        
p = Person()

class Francis(Person):
    def __init__(self):
        super().__init__()
        print("My name is Francis")

        
f = Francis()

As you can see, it gave an expected result.

New Person
My name is Francis

But a slight change will trigger the system and make a program go wrong.

For example:

class Person():
    def __init__(self):
        print("New Person")


p = Person()

class Francis(Person):
    def __init__(self):
        super().__init__():
            print("My name is Francis")


f = Francis()

That small change already made the code faulty. The error is invalid syntax. Honestly, how is this possible? I'm honestly surprised to see that a small change makes a big difference in the world of programming (yes, including Python).

  • I think it's Python itself. The programming world is strictly based on code and honestly can ruin your day, – Migs Panganiban Sep 17 '21 at 08:01
  • Why do you have a colon and then an indent after the call to super? It's not valid python syntax, you're not starting a new block – Iain Shelvington Sep 17 '21 at 08:01
  • Sometimes while writing a program in Python, you might accidentally use invalid syntax. – Migs Panganiban Sep 17 '21 at 08:04
  • 1
    I'm not sure what your point is? Syntax is fundamentally important in all languages, if your code is not syntactically correct then the compiler/interpreter will not be able to parse your code – Iain Shelvington Sep 17 '21 at 08:08
  • Consider code as a DNA chain, imagine you randomly modify something in such a chain and its effects. – Szabolcs Sep 17 '21 at 08:12
  • Indentation is important part of Python syntax. So change in indentation can make your code act differently or even raise an error. – buran Sep 17 '21 at 08:21
  • 2
    Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) Different error, but accepted answer is doing great jon at explaining why indentation matters in Python. – buran Sep 17 '21 at 08:22

1 Answers1

1

At the highest level your question can be answered by saying every language has a syntax and that syntax must be followed in order for the language "to work". In Python whitespace, specifically the whitespace at the beginning of a line, is very important for the interpreter to determine what your code is supposed to do. In your example you provide an example of an unexpected indent and a misplaced colon. You have incorrectly signaled to the interpreter that a new code block should begin and thus an error is shown to you.

While Python is a very syntactically forgiving language compared to predecessors like COBOL or C it is important to remember that even still tiny variations in the way a block of code is written can lead to immense changes in the code's behavior. Consider these two code snippets:

x=0
while x<10:
    print(x)
    x += 1

and

x=0
while x<10:
    print(x)
x += 1

The first will print out the digits 0-9 while the second will remain stuck in an infinite loop all due to a simple indentation error. I recommend reading up on Python's syntax.

0x263A
  • 1,807
  • 9
  • 22