Skip to content Skip to sidebar Skip to footer

Python: Variables In A Function With Dot Preceded By The Function Name

I need to understand this concept wherein we can use the dot (.) in the variable name within a function definition. There's no class definition nor a module here and Python is not

Solution 1:

From official documentation:

Programmer’s note: Functions are first-class objects. A “def” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def.

So, function is an object:

>>> f.__class__
<class'function'>
>>> f.__class__.__mro__
(<class'function'>, <class'object'>)

... and it means that it can store attributes:

>>> f.__dict__
{'language': 'Python', 'author': 'sunder'}
 >>> dir(f)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'author', 'language']

Post a Comment for "Python: Variables In A Function With Dot Preceded By The Function Name"