Python Forum
syntaxerror when entering a constructor - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: syntaxerror when entering a constructor (/thread-28785.html)



syntaxerror when entering a constructor - MaartenRo - Aug-03-2020

Hi,

I am working on an assignment for Python for Dummies and i have to enter a code but somehow it doesn't work. What am i doing wrong? And should the two blank lines between the codelines be only one sentence? Any input is appreciated!
The code in the book is:

class MyClass:
     Greeting = ""


     def __init__(self, Name="daar"):
        self.Greeting = Name + "!"


     def SayHello(self):
        print("Hallo {0}".format(self.Greeting))
When i enter this code in the Shell i get this:

>>> class MyClass:
	Greeting = ""

	
>>> def__init(self, Name="daar"):
	
SyntaxError: invalid syntax
>>> 



RE: syntaxerror when entering a constructor - buran - Aug-03-2020

note that there is space between def and __init__ on line 5
line 2 is redundant

also note that when you enter the code in interacative shell you want __init__ method to be indented as part of the class.
better write code in py file, not in interactive shell


RE: syntaxerror when entering a constructor - MaartenRo - Aug-03-2020

thanks, inserting the space made it work