Do you know what the Python ๐ `type` is?
But do you _really_ know what it is?
Here is a valuable thread that will give you insights into what `type` is and does.
Let's go ๐๐งต
What does `type` do?
You might say that `type` takes an object and returns the type of the argument.
Here are some examples:
Fine, so `type` is a function!
Or is it..?
Take a look at the example above.
`sum` is a built-in function and `type(sum)` returned โbuilt-in function or methodโ.
So, logically, if `type` is a (built-in) function, the type of `type` should give the same thing:
But it said that the type of `type` is... `type`? ๐คฏ
How's that?
What does that even mean?
Well, let's take a step back and look at `str` and `int`:
Interesting!
So `int` and `str` are built-in functions that convert things to integers and strings, respectively.
Right?
Well, let's check their type!
As it turns out, the type of `int` and `str` are also... `type`!
Ok, so these things seem to be connected!
Let's take one more step back.
Let's look at what the type of `3` is and to what `int` is:
Hey, `type(3)` and `int` look like the same thing!
Heck, they _are_ the same thing:
So `type(3)` and `int` are the same thing!
That's interesting!
So, `int` is not a function, and it builds integers, so `int` is like an integer factory!
That's why `int` shows like โclass intโ, because it is a class.
This applies to `int`, `str`, `list`, `tuple`, `dict`, ...
So, all these objects are the objects that represent the classes.
All these objects are factories of a specific kind.
`str` is a string factory.
`dict` is a dictionary factory.
Your own custom classes, are also factories:
But that brings us close to the end!
There's a bunch of objects, that we use regularly, that are just factories.
Fine, fine.
So, if all these objects are factories, maybe that's their type!
Their type could be โfactoryโ!
If something has the type โtypeโ, it means it's a factory.
Ok, so checking the type of `type` again shows that the type of `type` is... `type`.
So `type` is also a factory!
So what does `type` produce?
Let's see:
So `type` is producing other factories...
Meaning `type` is a factory of factories! ๐คฏ๐คฏ๐คฏ
I mean, you can even create new classes dynamically with `type`! (Not that you generally should...)
Here's the `Person` class from before:
And that's it, for now!
Recap:
๐ย `int`, `str`, `list`, ..., are NOT functions, but classes. Akin to the ones you create with the `class` keyword.
๐ These are like โfactoriesโ of objects.
๐ `type` accepts objects and returns classes, so `type` is a factory of factories!
I hope you found value in this tweet!
If you did, follow me (@mathsppblog) for more content like this!
There's plenty of amazing Python ๐ content in store!
Also, consider retweeting the first tweet of this thread if you learnt something new ๐
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.