__closure__ magic function in Python Last Updated : 11 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Almost everything in Python is an object, similarly function is an object too and all the function objects have a __closure__ attribute. __closure__ is a dunder/magic function i.e. methods having two underscores as prefix and suffix in the method name A closure is a function object that remembers values in enclosing scopes even if they are not present in memory. The __closure__ attribute of a closure function returns a tuple of cell objects. This cell object also has an attribute called cell_contents, which returns the contents of the cell. Syntax: closure_function.__closure__ Example: Python # this is a nested function def gfg(raise_power_to): def power(number): return number ** raise_power_to return power raise_power_to_3 = gfg(3) print(raise_power_to_3.__closure__) print(raise_power_to_3.__closure__[0].cell_contents) Output: (<cell at 0x7f34ba2725e0: int object at 0x7f34bde02720>,) 3 In the above example, the nested function power has __closure__ attribute associated with it and it returns a tuple of cell objects. The cell_contents attribute returns the value 3 as it was closed inside the cell object. Comment More infoAdvertise with us Next Article Python Inner Functions A ashisholism Follow Improve Article Tags : Python Python-Built-in-functions Practice Tags : python Similar Reads dir() function in Python The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging.Syntaxdir([object])Parameters: object (optional): Any Python object (lik 3 min read dir() function in Python The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging.Syntaxdir([object])Parameters: object (optional): Any Python object (lik 3 min read Function aliasing in Python In Python, we can give another name of the function. For the existing function, we can give another name, which is nothing but function aliasing. Function aliasing in Python In function aliasing, we create a new variable and assign the function reference to one existing function to the variable. We 2 min read Python Inner Functions In Python, a function inside another function is called an inner function or nested function. Inner functions help in organizing code, improving readability and maintaining encapsulation. They can access variables from the outer function, making them useful for implementing closures and function dec 5 min read Python Inner Functions In Python, a function inside another function is called an inner function or nested function. Inner functions help in organizing code, improving readability and maintaining encapsulation. They can access variables from the outer function, making them useful for implementing closures and function dec 5 min read __import__() function in Python __import__() is a built-in function in Python that is used to dynamically import modules. It allows us to import a module using a string name instead of the regular "import" statement. It's useful in cases where the name of the needed module is know to us in the runtime only, then to import those mo 2 min read Like