Tools, FAQ, Tutorials:
Functions Are Objects Too
Are functions objects in Python?
✍: FYIcenter.com
Yes, all functions are objects of "function" type in Python?
You can verify this with the following Python code:
>>> def x():
... print("Hello world!")
...
>>> type(x)
<class 'function'>
In other words, the "def x()" statement block performed two activities:
Like any other types of objects, you can use the "dir()" to list its members:
>>> def x():
... print("Hello world!")
...
>>> dir(x)
['__annotations__', '__call__', '__class__', '__closure__', '__code__',
'__defau lts__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__get__', '__getattribute__', '__globals__',
'__gt__', '__hash__', ' __init__', '__init_subclass__', '__kwdefaults__',
'__le__', '__lt__', '__module_ _', 'Python Tutorials', '__ne__', '__new__',
'__qualname__', '__reduce__', '__reduce_ex_ _', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__']
You can call (execute) the function object by call its "__call__()" function:
>>> def x():
... print("Hello world!")
...
>>> x.__call__()
Hello world!
⇒ '__dict__' Dictionary in Function Object
⇐ Function Parameters Assigned with Object References
2018-02-08, ∼1935🔥, 0💬
Popular Posts:
How To Pad an Array with the Same Value Multiple Times in PHP? If you want to add the same value mul...
How to install "C++/CLI Support" component in Visual Studio? I need to build my Visual Studio C++/CL...
How to use the "Ctrl-p Ctrl-q" sequence to detach console from the TTY terminal of container's runni...
How to add request URL Template Parameters to my Azure API operation 2017 version to make it more us...
What is EPUB 3.0 Metadata "dc:description" Element? EPUB 3.0 Metadata "dc:description" is an optiona...