print(dir(this_function_is_an_object))
Why does this work, even though my function takes a parameter of num?
print(dir(this_function_is_an_object))
Why does this work, even though my function takes a parameter of num?
When we define a function, its name has to be referred to directly if we want to talk about the function as an object. The moment we add the () we are invoking the function.
Consider this simple example:
def hello():
print('hi')
print(hello)
# output: <function hello at 0x7fe5a1947830>
hello()
# output: hi
#or
#let's re-define the function
def hello():
return "hello"
print(hello)
# output: <function hello at 0x7fe5a1947830>
print(hello())
#output: hello
print("hello")
#output: hello
print( "hello"== hello() )
# True
print( "hello" = hello )
# False
We are not very likely to use the built-in dir()
function in a program. It’s more of an inspection tool for use in the interactive shell. We use the function to find the contents of a module. Try it on the math
module…
>>> import math
>>> dir(math)
This gives us a list object populated with the attribute names of the module.
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.