“This constructor takes no arguments” error in __init__

前端 未结 3 1430
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 14:02

I\'m getting an error while running the following code:

class Person:
  def _init_(self, name):
    self.name = name

  def hello(self):
    print \'Initiali         


        
3条回答
  •  再見小時候
    2020-12-01 14:33

    The method should be named __init__ to be a constructor, not _init_. (Note the double underscores.)

    If you use single underscores, you merely create a method named _init_, and get a default constructor, which takes no arguments.

提交回复
热议问题