On most systems, as soon as you type the magic words `python` on a CMD prompt or a terminal and once the >>>
prompt shows up, if you execute the dir()
command,
this is what you most likely see -
When you have no packages or libraries imported, by default, the “dunder” or “double under” names that Python injects, show up.
Let’s look at each of them in turn :
a) __annotations__
By default, the annotations object is empty. It’s an empty dictionary, to be precise.
So, on its own, it doesn’t give us any useful information. But once we start creating some methods, things start getting a little interesting -
That was underwhelming….We created a function. So why isn’t it getting populated? What is it for?
Let’s make one small tweak -
So, when we provide a type hint to one/more of the parameters, the __annotations__ dictionary starts coming to life. Here in particular, the parameter ‘a’ is populated in the __annotations__ dictionary with its hinted type i.e. int.
There is also one more utility of this dunder object :
If a return type is provided to a function, even that gets added to its corresponding __annotations__ dictionary.
b) __builtins__
Every module’s global namespace automatically gets a name __builtins__
that points to Python’s built-in namespace. Depending on context, this is either:
The actual
__builtins__
module (as you’ll see in the REPL),Or a plain dictionary mapping built-in names to their objects (in some compiled modules) (when does this happen? - see below)
The entire list of built-in commands and errors etc. can be listed using dir
on __builtins__
like so -
(Sleepy now - I’ll finish up the rest tomorrow)