Rodrigo ๐Ÿ๐Ÿš€ Profile picture
Take your Python ๐Ÿ skills to the next level ๐Ÿš€!

Sep 23, 2021, 14 tweets

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.

Keep scrolling