0
Method calls ( Python )
What is the method calls ?
2 Réponses
+ 3
Method:
————
def my_func():
print("spam")
print("spam")
print("spam")
her call
———-
my_func()
Result:
————
spam
spam
spam
>>>
+ 1
It is a way to run the code defined by the function. It calls when you add the paranthesis att the end of the function name, and passes the arguments inside the paranthesis to the function code. Without the paranthesis it acts much like an ordinary object in Python like this:
def f(x):
return x ** 2
print(f(4))
g = f
f.a = "Hello world"
print(g.a, g(4))
If you instead create a function inside a class, it becomes a class method, and you have to call it through an object of that class: my_object.f(4).