Getting NameError when calling function in constructor

前端 未结 4 722
粉色の甜心
粉色の甜心 2021-01-21 07:31

I ran the code below, by calling the function in the constructor

First --

>>> class PrintName:
...    def __init__(self, value):
...      self.         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 07:48

    You need to call self.printName since your function is a method belonging to the PrintName class.

    Or, since your printname function doesn't need to rely on object state, you could just make it a module level function.

    class PrintName:
        def __init__(self, value):
            self._value = value
            printName(self._value)
    
    def printName(value):
        for c in value:
        print c
    

提交回复
热议问题