Python Forum
syntaxerror when entering a constructor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
syntaxerror when entering a constructor
#1
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
>>> 
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
thanks, inserting the space made it work
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,073 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  problem in entering address of a file in input akbarza 0 649 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 930 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,813 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  How to Stop Sentinel Value from Entering Final Output ZQ12 3 3,225 Nov-11-2019, 07:25 AM
Last Post: perfringo
  error in constructor overriding in python3 srm 1 1,791 Jul-18-2019, 12:21 PM
Last Post: ichabod801
  This constructor takes no arguments Friend 2 5,316 Jun-26-2019, 02:54 PM
Last Post: Friend
  Error when entering letter/character instead of number/integer helplessnoobb 2 7,058 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  class constructor with dataframe UGuntupalli 2 2,306 Jun-11-2019, 10:50 PM
Last Post: UGuntupalli
  Overload of constructor psosmol 2 2,799 Apr-17-2019, 05:10 AM
Last Post: psosmol

Forum Jump:

User Panel Messages

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