在Python中,函数调用时通常需要加括号,因为括号用于传递参数给函数。如果函数没有参数或者所有参数都有默认值,那么括号里可以不填内容。
例如,如果有一个函数定义如下:
def greet(name="World"):
print(f"Hello, {name}!")
greet() 输出 "Hello, World!"
如果需要传递参数,则必须加括号:
greet("Alice") 输出 "Hello, Alice!"
如果函数返回另一个函数,那么在返回的函数后加括号表示调用该函数。例如:
def outer_function():
def inner_function():
print("This is an inner function.")
return inner_function
inner = outer_function() outer_function 返回 inner_function
inner() 输出 "This is an inner function."
希望这能解答你的问题,