ParenthesesOnlyIfArgument
When calling a function without arguments, it is not necessary to use parentheses.
() are optional for function calls without arguments
() are mandatory even for function calls without arguments
CorrectionHere is what's right.
Parentheses are required in Python to execute a function call. When calling any function, even a function without arguments, parentheses are required. If no parentheses are added, then the expression evaluates to a function, rather than to the result of calling the function.
def f():
return 42
print(f) # the function f
print(f()) # the result of calling function f
OriginWhere could this misconception come from?
Students may have prior knowledge of languages where parentheses are indeed optional for argument-less function calls.
SymptomsHow do you know your students might have this misconception?
A student may write code as follows, assuming that f
will be invoked
def f():
return 42
print(f)
ValueHow can you build on this misconception?
At first this misconception may look purely superficial. However, it can be surprisingly deep.
In languages where functions are values, it is essential to understand the difference between accessing the function as a value (f
), and invoking a function (f()
).
In Python we may use parentheses next to any name that refers to a function to invoke that function:
def f():
return 42
g = f
print(g()) # displays 42
Language
Concepts
Expressible In
Related Misconceptions
Other Languages
Literature References
Repository mining study (BlueJ Blackbox)
250000+ students across the world
Qualitative (survey of instructors and students)
Unknown number of undergraduate students, professors, SIGCSE members