class MyClass:
def say():
print("hello")
mc = MyClass()
mc.say()
I am getting error: TypeError: say() takes no arguments (1 given)
. What I am doing wrong?
class MyClass:
def say():
print("hello")
mc = MyClass()
mc.say()
I am getting error: TypeError: say() takes no arguments (1 given)
. What I am doing wrong?
This is because methods in a class expect the first argument to be self
. This self
parameter is passed internally by python as it always sends a reference to itself when calling a method, even if it's unused within the method
class MyClass:
def say(self):
print("hello")
mc = MyClass()
mc.say()
>> hello
Alternatively, you can make the method static and remove the self
parameter
class MyClass:
@staticmethod
def say():
print("hello")
mc = MyClass()
mc.say()
>> hello