I'm new to Python. I couldn't understand the idea of self, and I stumbled on it so I made a note.
class HelloWorld:
def __init__(self):
self.message = 'Hello,World'
def gree(self):
print(self.message)
import hello
h = hello.HelloWorld
h.gree()
With this code, h.gree () gives the error "No value for argument'self' in unbound method call". The content of the error is that there is no argument self of the function gree
Isn't it necessary to specify self as an argument when calling a function? I stumbled.
Solved by making the code of sample.py below
import hello
h = hello.HelloWorld()
h.gree()
reference
h = hello.HelloWorld
print(type(h))
h = hello.HelloWorld()
print(type(h))
<class 'type'>
<class 'hello.HelloWorld'>
Substitute class definition for hello.HelloWorld assign an instance of the class with hello.HelloWorld ()
I understand that self does not work with hello.HelloWorld because it holds the instance information of the class.
Recommended Posts