Intern
210
What is the difference between the function func and its call func() in Python?
Answer from AI
sobes.tech AI
func is the name of the function (the function object in memory).
func() is a call to the function, executing its code.
func |
func() |
|
|---|---|---|
| Object type | Function | Result of function execution |
| Action | Reference to the function object | Executing the function code |
| Result | The function object itself | The value returned by the function (None if no explicit return) |
# Example 1: Using func as a function object
def greet(name):
return f"Hello, {name}!"
my_function = greet # my_function now references the same function object as greet
print(type(greet))
print(type(my_function))
# Example 2: Calling the function func()
result = greet("World")
print(result)
# Example 3: Usage difference
def add(a, b):
return a + b
print(add) # Prints the function object: <function add at ...>
print(add(2, 3)) # Calls the function and prints the result: 5