Callable#
A function in Python is a reusable block of code that performs a specific task. It can be executed with different sets of arguments, allowing for code abstraction and modularity by encapsulating operations into a single, callable unit.
Parameters#
A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept.
Find out more:
Parameters page.
There are such types of parameters in python:
Name |
Description |
Syntax |
|---|---|---|
Positional-only |
Parameters that can be specified only positionally. |
Parameters before the symbol |
Positional-or-keyword |
Can be specified positionally as well as through keyword arguments. |
Default parameters of the function |
Var-positional |
Parameter that takes values of all positional arguments that don’t correspond to the positional-only or positional-or-keyword parameters. |
Parameter name begins with the |
Keyword-only |
Can be specified only through keyword arguments. |
Parameters that follow the Var-positional argument or just |
Var-keyword |
Parameter that takes values of all keyword arguments that don’t correspond to the positional-or-keyword or keyword-only parameters. |
Parameter name begins with |
You can define default values for parameters just by using syntax <parameter>=<default value>. But of you have defined default values for positional-only or for positional-or-keyword parameter all following parameters of these groups should have default values.
The following cell shows the definition of a function that contains all types of parameters, with default values defined for some of them, arranged in a way that forms a valid Python expression.
def some_function(
positional_only1,
positional_only2,
/,
positional1,
positional2=None,
*args,
keyword_only1=None,
keyword_only2,
**kwargs
):
pass
User-defined functions#
The set of attributes specific for user-defined functions is represented in the following table:
Category |
Attribute |
Meaning |
|---|---|---|
Read-only |
|
The dictionary that hold’s the function’s global variables |
Writable |
|
The funciton’s documentation string, or |
|
The funciton’s name |
|
|
The funciton’s qualified name |
|
|
The name of the module the function was defined in, or |
|
|
A tuple containing default parameter values, or |
For full list of attributes check: special read only attributes and special writable attributes.
The following cell defines the function and displays the values of some of the attributes specified user-defined attributes.
def hello_function(val = 10):
pass
print(hello_function.__name__)
print(hello_function.__module__)
print(hello_function.__qualname__)
hello_function
__main__
hello_function
Note: The python documentation doesn’t specify it explicitly, but it appears that the instance methods are a kind of subset of the user-defined functions and generally have the same attributes.
The following cell defines the MyClass.fun method and displays the values of sertain attributes.
class MyClass:
def fun(self):
"""Function's doc-string"""
global hello
my_instance = MyClass()
print(my_instance.fun.__name__)
print(my_instance.fun.__doc__)
fun
Function's doc-string
{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', 'def hello_function(val = 10):\n pass\n\nhello_function.__name__', 'def hello_function(val = 10):\n pass\n\nhello_function.dir', 'def hello_function(val = 10):\n pass\n\nhello_function.dir()', 'def hello_function(val = 10):\n pass\n\ndir(hello_function)', 'def hello_function(val = 10):\n pass\n\nprint(hello_function.__name__)\nprint(hello_function.__module__)', 'def hello_function(val = 10):\n pass\n\nprint(hello_function.__name__)\nprint(hello_function.__module__)\nprint(hello_function.__qualname__)', '---', 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\nmy_instance.fun.__name__', 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', 'class MyClass:\n def fun(self):\n """Function"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n global hello\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)\nprint(my_instance.fun.__globals__)'], '_oh': {1: 'hello_function', 4: ['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__getstate__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__type_params__'], 8: 'fun'}, '_dh': [PosixPath('/home/fedor/Documents/code/python/core/syntax')], 'In': ['', 'def hello_function(val = 10):\n pass\n\nhello_function.__name__', 'def hello_function(val = 10):\n pass\n\nhello_function.dir', 'def hello_function(val = 10):\n pass\n\nhello_function.dir()', 'def hello_function(val = 10):\n pass\n\ndir(hello_function)', 'def hello_function(val = 10):\n pass\n\nprint(hello_function.__name__)\nprint(hello_function.__module__)', 'def hello_function(val = 10):\n pass\n\nprint(hello_function.__name__)\nprint(hello_function.__module__)\nprint(hello_function.__qualname__)', '---', 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\nmy_instance.fun.__name__', 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', 'class MyClass:\n def fun(self):\n """Function"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n global hello\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)\nprint(my_instance.fun.__globals__)'], 'Out': {1: 'hello_function', 4: ['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__getstate__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__type_params__'], 8: 'fun'}, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7604cc1482f0>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7604cc14a900>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7604cc14a900>, 'open': <function open at 0x7604d12523e0>, '_': 'fun', '__': ['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__getstate__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__type_params__'], '___': 'hello_function', '__vsc_ipynb_file__': '/home/fedor/Documents/code/python/core/syntax/callable.ipynb', '_i': 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', '_ii': 'class MyClass:\n def fun(self):\n """Function"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', '_iii': 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', '_i1': 'def hello_function(val = 10):\n pass\n\nhello_function.__name__', 'hello_function': <function hello_function at 0x7604b489d6c0>, '_1': 'hello_function', '_i2': 'def hello_function(val = 10):\n pass\n\nhello_function.dir', '_i3': 'def hello_function(val = 10):\n pass\n\nhello_function.dir()', '_i4': 'def hello_function(val = 10):\n pass\n\ndir(hello_function)', '_4': ['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__getstate__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__type_params__'], '_i5': 'def hello_function(val = 10):\n pass\n\nprint(hello_function.__name__)\nprint(hello_function.__module__)', '_i6': 'def hello_function(val = 10):\n pass\n\nprint(hello_function.__name__)\nprint(hello_function.__module__)\nprint(hello_function.__qualname__)', '_i7': '---', '_i8': 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\nmy_instance.fun.__name__', 'MyClass': <class '__main__.MyClass'>, 'my_instance': <__main__.MyClass object at 0x7604b48bc980>, '_8': 'fun', '_i9': 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', '_i10': 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', '_i11': 'class MyClass:\n def fun(self, val: 10):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', '_i12': 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__defaults__)', '_i13': 'class MyClass:\n def fun(self):\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', '_i14': 'class MyClass:\n def fun(self):\n """Function"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', '_i15': 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n pass\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)', '_i16': 'class MyClass:\n def fun(self):\n """Function\'s doc-string"""\n global hello\n\nmy_instance = MyClass()\n\nprint(my_instance.fun.__name__)\nprint(my_instance.fun.__doc__)\nprint(my_instance.fun.__globals__)'}
Qualified name#
Qualified name is a dotted “path” of the global module’s namespace: For an object that defined in the current module.
Callable objects store their qualified names in the __qualname__ attribute.
The following cell defines nested classes structure:
class Outer():
class Inner():
def method(self):
pass
The __qualname__ attribute values for different elements of the structure:
Outer.__qualname__
'Outer'
inner = Outer.Inner
inner.__qualname__
'Outer.Inner'
inner.method.__qualname__
'Outer.Inner.method'
Note: The __qualname__ for imported objects does not include the information about the module.
The following cell shows the __qualname__ of the unittest.TestCase.
from unittest import TestCase
TestCase.__qualname__
'TestCase'