Transcript
This transcript was autogenerated. To make changes, submit a PR.
Hi, I'll be speaking on the hidden potential of Python's Dunder method.
About me, I'm a software engineer with over five years of experience in
working for some companies globally.
I'm very passionate about sharing knowledge and I do
this through technical writing.
I'm also an active contributor to the open source community, and socials below.
What a Dunder method.
So before I dive into our Dunder method, I would like to
just take us a few steps back.
So maybe most of us here have worked with Python classes and
object oriented programming before.
You've probably written the double underscore init method
to initialize your object.
Or perhaps you've come across the double underscore string method while trying to
make your object print something readable.
But let me ask you this.
Did you ever stop and wonder what's really going on behind those double underscores?
Or are they just some really quirky convention?
They are part of something much bigger because they help Python work
the way it does and they make our code really flexible and powerful.
So imagine you're adding two numbers in Python.
That's probably simple, like you write five plus three and Python
instantly knows to return it.
Now, what if you want to add to object you've created in your own class?
At first, Python might give you an error, but here's the thing.
You can teach Python how to understand what's an error.
The plus operator means for your object, just like it does for number.
How can we do that?
We can do that using the Dunder method.
So this might sound like some really advanced Python, wizardry thing, but I
promise it's not intimidating as it seems so, Dunder methods or magic methods,
as often as most people might call it.
They're just basically tools, right?
That built into Python.
They let you define objects, how your objects should behave when they are
used in familiar ways, like being added or compared and maybe, subtracted.
So when methods, these methods, aren't just something really technical.
They're just practical ways to make your code really clean, smart.
And look like Python.
So for example, think about this.
Have you ever used the length method function to get the size of
a list of string behind the scene?
Python isn't just automatically going to know, the length, right?
It's going to call the Dunder method, double underscore length.
And the same goes for printing an object.
Python calls the double underscore string method to figure out what to display.
This methods are quietly doing a whole lot of heavy lifting to make Python feel
as seamless and intuitive as it does.
But here's where the things get even more exciting.
This Dunder methods aren't just limited to Python built in types.
You can use them in your own class to create objects that
feel just as natural to use.
So you imagine building a library.
Where users can treat your object like they treat the list.
Like the trees list or numbers, they can add, They can compare, they can
iterate over them without ever having to read a really complicated API docs.
let me just give you a glimpse of, how this works.
we've maybe probably worked with popular libraries like NumPy, In
NumPy, you can add arrays together by simply using the add operator, like I
should, like I'm showing in the slide.
And then the NumPy uses Donda methods like the underscore adds to make this happen.
Or maybe, we've probably worked with the SQL alchemy and it's a
library for working with database.
Yeah.
It's basically helps you, lets you build SQL queries using Python
comparison operator, like the double equal to, this is not really just some
hack it's done the methods at play.
So done the methods basically uses the.
Double underscore equal, method.
And now you can see that this library feels so intuitive because they've really,
yeah, using the power of Python's methods to blend seamlessly with the language.
so here we see.
that this libraries used on the methods to integrate Python syntax without
their objects, but they are making the objects feel very easy to use.
So done the methods goals beyond using the double underscore in it, or.
The double underscore REPL for creating a clustering representation of your object.
So we also have, the double underscore get item, which enables you to get an
index based access of your, objects.
Or we have the double underscore iterate, I T E R iteration, which basically
helps you or makes your object iterable.
What makes Dunder objects really exciting is how they give you
ability to control how your objects behave in nearly any context.
So you can define how they should respond to arithmetic comparison.
Or even built in function.
And once you understand them, you realize that they are not just a feature
in Python, they are a very key part of Python design philosophy, which is
really simplicity and expressiveness.
So now you might be wondering.
Why should I care about Dunder Method?
The answer is very simple.
You are building applications, if you are building applications, or
libraries, or frameworks, using Dunder Method can make your code
really intuitive for others to use.
It means like you're writing an API that feels really natural and familiar
reducing the learning curve for users.
And if you work on your own project, Dunder methods lets you write code
really cleaner and really usable.
So today we are going to dive into how this Dunder method works and, we're going
to see how we can use it to, Build our own, custom class using done the methods.
if you're just a beginner or you already have some experience working with
Python, this talk is just going to give you like a really fresh perspective.
how they work.
like I said, there are core features of Python that allows instances of class to
seamlessly interact with Python's built in functions and operator of the language.
So the term done that just comes from the word double underscore,
because this method starts and end with two underscores, such as
underscore string and underscore in it.
why they may seem mysterious at the first glance, they are well defined
rules and they are invoked at a specific time in the Python interpreter.
So at the core, Dunder methods are not actually magical in the sense of being
mysterious or some very arbitrary thing.
They are methods that are.
in Python's interpreter and and Python interpreter implicitly
calls them when like a specific action occurs in your program.
So for example, when you want to assess the length of an object.
there are some characteristics of Python's Dunder method, right?
Like I said earlier, it's a built in, it has double underscore.
you can't Dunder methods are tied to a specific operation or functions.
So such as like the, double underscore call or double underscore add, right?
So you can't just come up with your own, or you can't invent your own
Dunder methods you can't create a class, for example, like what I did here.
And name your dunder methods like create a method and just give it a custom name,
understand double underscore custom, because in the real sense, Python will
not be able to recognize your methods and there, it will make much sense.
this methods.
You, for you to be able to use Python standard method, you have to use
what's already existing and you can find the least of all these methods
in the Python's documentation.
so we're going to create our own custom class, right?
It's called points.
when we create a custom class called points, like we, most
of us here do, we, we can.
We already used the double underscore in it, basically.
And what this does is that it helps, when we initialize our class.
it's basically, passes or initializes the object attributes,
what we passed in the constructor.
now We might want to print out the class, right?
Like print out the class.
And then when we use print automatically, Python uses the string double
score double underscore string or double underscore repel metal to
define how this object should be represented as strings by default.
Python shows this object types in look type and the location in
memory, which isn't very helpful, like what we have here in the slides.
to use, to display, more readable, Data.
When we try to print our class, we can create by, or we can implement
the REPL method to provide like a more meaningful string representation.
So here we, we added the REPL, method.
And then when we instantiated our class again and printed out points,
what happened is that we were.
We could see the, a better output, which is points and two, two and three.
So basically REPL just Python.
When we call the print methods, Python knows to call REPL and
represent the string in a proper way.
what if we want to add.
We instantiate two classes that are class twice, right?
So for example, here we have 1 and 2 and we decide to add
these two classes together.
What happens?
We get an error from Python saying unsupported operand types,
because we actually cannot add these two classes together.
the way the, add operator works by default, it works with numbers or string.
So to make the add work with our class, we have to define the add on the methods.
In our point class.
So let's define the addDundee method.
like you see here, we defined it and we said, whenever the
plus operator is called, we try to add the classes together.
And now when we try to instantiate again, what happens is that we
add the point one and point two class and we get an actual output.
now we've seen that we've been able to tell Python how to handle the
plus operator in the, the point object, because now you can add
both X and Y coordinates of the two points and return a new point object.
let's look at the plus, the equal to operator.
we can see that if we try to, like we instantiated two class, the two objects,
two points object earlier, right?
If we try to compare both objects together, right?
We see, we try to compare 0.
1 and 0.
2. We got true, but 0.
1 and 0.
3, we got false, right?
we got false for both of them.
And oh, I'm sorry, I missed something here.
So if we try to instantiate the point objects twice and compare 0.
1 and 0.
2, we see that it is not equal, right?
So that's because if we don't have the, don't, equal to method, right?
Equality methods.
So if we define that like we did here, and we now instant, we try to compare them.
And we see that 0.
1 and 0.
2 methods.
now equal 3 are not equal.
So basically now we've been able to create, the fact that we created the
equality done the methods now means that when we use the double equal to,
it now works on our point objects, comparing X and Y values for equality.
So another.
Methods that we can create is the double underscore multiplication,
multiple collision, done the methods.
So like you see here, we added the multiplication methods.
And what this does is that when we try to multiply our class with, in
number, It brings out an actual output.
So we see that the multiplication operator now scales the point by multiplying it
our coordinates by the scalar value, which we passed here and three, which
is the scalar value we passed here.
We can also, yeah, several done the methods, but another
very interesting one is the.
let's implement this iterator method to make the point objects iterable.
So you can look through when we call the, when we implement this method,
what it does basically is that we can loop over like the coordinates.
So we can see how we, we did that here.
Okay.
but what happened is that now, when we try to loop over our, point objects, we make
them feel like native Python collections.
So now we can be able to loop through them.
Um, The next thing I'll be talking about is why Python Dundun methods
are really interesting to use.
we do not just want to use them everywhere in our code, right?
We do not want to abuse them.
So, this.
And, I think like we need to use them when necessary.
So they are really powerful.
And personally, I think like you should use them thoughtfully.
So you shouldn't like overuse them or misuse them.
And because they can make the code really confusing and unpredictable
or probably hard to maintain.
So there are several guides on where not to use Dunder method.
And, so for example, don't use them to solve like really simple problems.
So you can see here in our calculator class, right?
We just had, an add.
we have the Dunder method add.
To multiply, the, to multiply data, to multiply the values in the calculator.
So this is quite confusing because we shouldn't use that, if we are
using the ad, it should depicts adding values together, Another
thing too, is using the call.
like I said earlier, we don't want to, we don't want to use the, there's a
double underscore call method, right?
We don't want to implement that in our class because that's
quite unnecessary, right?
It's doing, it's, we might not really need it, right?
Or imagine implementing a call method that is actually adding numbers together.
So it doesn't make any sense.
So we want to use Dunder methods.
That depicts the right thing in your code.
So they don't become really confusing.
Also, you do not want to invent your own dunder methods.
Like I said earlier, if you declare a dunder method that does not exist
in Python native, in the native Python, it won't work, right?
It's just going to be like, you're going to be confusing everyone
because someone can hop on your code and see it and get confused because
this doesn't really do anything.
instead just use a regular method.
Also, operator, we don't want to make operator overload and really confusing.
like I said earlier, you don't want to just, use an add method to, Multiply
several, numbers, you don't want to use a multiply multiplication, done the
methods to add several numbers, right?
you want to keep it really consistent and very predictable.
So another thing is that you don't want to overuse it in your classes.
So you don't want to have a class that uses all the.
The method just makes your class really confused.
You want to use them only when necessary.
And another thing is that you don't want to sacrifice
readability for cleverness, right?
So if you have a lot of this done, the methods, your class, it might
look cool, but it might be very difficult for others to maintain.
in conclusion,
using done, the methods It's really powerful, right?
So we've seen how we're able to combine Dunder methods to be able to build
the point class earlier and to make it look really rich and intuitive.
So these are like techniques that you can use to design objects that makes it feel
really natural in our applications domain.
For example, If you are working with a graphic library, you might want
to use something like the Dunder method underscore Dunder methods
add, or you're working like with a game development library, right?
It might make sense to use the equality Dunder methods for maybe
your collision detection, right?
So Dunder methods bridge the gap between your custom object and
Python's built in capabilities.
Allowing you to design APIs, an application that looks really
powerful, but also really easy to use.
Thank you.
Hi, I'll be speaking on the hidden potential of Python's Dunder method.
About me, I'm a software engineer with over five years of experience in
working for some companies globally.
I'm very passionate about sharing knowledge and I do
this through technical writing.
I'm also an active contributor to the open source community, and socials below.
What a Dunder method.
So before I dive into our Dunder method, I would like to
just take us a few steps back.
So maybe most of us here have worked with Python classes and
object oriented programming before.
You've probably written the double underscore init method
to initialize your object.
Or perhaps you've come across the double underscore string method while trying to
make your object print something readable.
But let me ask you this.
Did you ever stop and wonder what's really going on behind those double underscores?
Or are they just some really quirky convention?
They are part of something much bigger because they help Python work
the way it does and they make our code really flexible and powerful.
So imagine you're adding two numbers in Python.
That's probably simple, like you write five plus three and Python
instantly knows to return it.
Now, what if you want to add to object you've created in your own class?
At first, Python might give you an error, but here's the thing.
You can teach Python how to understand what's an error.
The plus operator means for your object, just like it does for number.
How can we do that?
We can do that using the Dunder method.
So this might sound like some really advanced Python, wizardry thing, but I
promise it's not intimidating as it seems so, Dunder methods or magic methods,
as often as most people might call it.
They're just basically tools, right?
That built into Python.
They let you define objects, how your objects should behave when they are
used in familiar ways, like being added or compared and maybe, subtracted.
So when methods, these methods, aren't just something really technical.
They're just practical ways to make your code really clean, smart.
And look like Python.
So for example, think about this.
Have you ever used the length method function to get the size of
a list of string behind the scene?
Python isn't just automatically going to know, the length, right?
It's going to call the Dunder method, double underscore length.
And the same goes for printing an object.
Python calls the double underscore string method to figure out what to display.
This methods are quietly doing a whole lot of heavy lifting to make Python feel
as seamless and intuitive as it does.
But here's where the things get even more exciting.
This Dunder methods aren't just limited to Python built in types.
You can use them in your own class to create objects that
feel just as natural to use.
So you imagine building a library.
Where users can treat your object like they treat the list.
Like the trees list or numbers, they can add, They can compare, they can
iterate over them without ever having to read a really complicated API docs.
let me just give you a glimpse of, how this works.
we've maybe probably worked with popular libraries like NumPy, In
NumPy, you can add arrays together by simply using the add operator, like I
should, like I'm showing in the slide.
And then the NumPy uses Donda methods like the underscore adds to make this happen.
Or maybe, we've probably worked with the SQL alchemy and it's a
library for working with database.
Yeah.
It's basically helps you, lets you build SQL queries using Python
comparison operator, like the double equal to, this is not really just some
hack it's done the methods at play.
So done the methods basically uses the.
Double underscore equal, method.
And now you can see that this library feels so intuitive because they've really,
yeah, using the power of Python's methods to blend seamlessly with the language.
so here we see.
that this libraries used on the methods to integrate Python syntax without
their objects, but they are making the objects feel very easy to use.
So done the methods goals beyond using the double underscore in it, or.
The double underscore REPL for creating a clustering representation of your object.
So we also have, the double underscore get item, which enables you to get an
index based access of your, objects.
Or we have the double underscore iterate, I T E R iteration, which basically
helps you or makes your object iterable.
What makes Dunder objects really exciting is how they give you
ability to control how your objects behave in nearly any context.
So you can define how they should respond to arithmetic comparison.
Or even built in function.
And once you understand them, you realize that they are not just a feature
in Python, they are a very key part of Python design philosophy, which is
really simplicity and expressiveness.
So now you might be wondering.
Why should I care about Dunder Method?
The answer is very simple.
You are building applications, if you are building applications, or
libraries, or frameworks, using Dunder Method can make your code
really intuitive for others to use.
It means like you're writing an API that feels really natural and familiar
reducing the learning curve for users.
And if you work on your own project, Dunder methods lets you write code
really cleaner and really usable.
So today we are going to dive into how this Dunder method works and, we're going
to see how we can use it to, Build our own, custom class using done the methods.
if you're just a beginner or you already have some experience working with
Python, this talk is just going to give you like a really fresh perspective.
how they work.
like I said, there are core features of Python that allows instances of class to
seamlessly interact with Python's built in functions and operator of the language.
So the term done that just comes from the word double underscore,
because this method starts and end with two underscores, such as
underscore string and underscore in it.
why they may seem mysterious at the first glance, they are well defined
rules and they are invoked at a specific time in the Python interpreter.
So at the core, Dunder methods are not actually magical in the sense of being
mysterious or some very arbitrary thing.
They are methods that are.
in Python's interpreter and and Python interpreter implicitly
calls them when like a specific action occurs in your program.
So for example, when you want to assess the length of an object.
there are some characteristics of Python's Dunder method, right?
Like I said earlier, it's a built in, it has double underscore.
you can't Dunder methods are tied to a specific operation or functions.
So such as like the, double underscore call or double underscore add, right?
So you can't just come up with your own, or you can't invent your own
Dunder methods you can't create a class, for example, like what I did here.
you're done the methods like create a methods.
And just give me the custom limb, understand double underscore custom
because in the real sense Python will not be able to recognize your methods
and they, it will make much sense.
this methods.
You, for you to be able to use Python standard method, you have to use
what's already existing and you can find the least of all these methods
in the Python's documentation.
so we're going to create our own custom class, right?
It's called points.
when we create a custom class called points, like we, most
of us here do, we, we can.
We already used the double underscore in it, basically.
And what this does is that it helps, when we initialize our class.
it's basically, passes or initializes the object attributes,
what we passed in the constructor.
now We might want to print out the class, right?
Like print out the class.
And then when we use print automatically, Python uses the string double
score double underscore string or double underscore repel metal to
define how this object should be represented as strings by default.
Python shows this object types in look type and the location in
memory, which isn't very helpful, like what we have here in the slides.
to use, to display, more readable, Data.
When we try to print our class, we can create by, or we can implement
the REPL method to provide like a more meaningful string representation.
So here we, we added the REPL, method.
And then when we instantiated our class again and printed out points,
what happened is that we were.
We could see the, a better output, which is points and two, two and three.
So basically REPL just Python.
When we call the print methods, Python knows to call REPL and
represent the string in a proper way.
what if we want to add.
We instantiate two classes that are class twice, right?
So for example, here we have 1 and 2 and we decide to add
these two classes together.
What happens?
We get an error from Python saying unsupported operand types,
because we actually cannot add these two classes together.
the way the, add operator works by default, it works with numbers or string.
So to make the add work with our class, we have to define the add on the methods.
In our point class.
So let's define the addDundee method.
like you see here, we defined it and we said, whenever the
plus operator is called, we try to add the classes together.
And now when we try to instantiate again, what happens is that we
add the point one and point two class and we get an actual output.
now we've seen that we've been able to tell Python how to handle the
plus operator in the, the point object, because now you can add
both X and Y coordinates of the two points and return a new point object.
let's look at the plus, the equal to operator.
we can see that if we try to, like we instantiated two class, the two objects,
two points object earlier, right?
If we try to compare both objects together, right?
We see, we try to compare 0.
1 and 0.
2. We got true, but 0.
1 and 0.
3, we got false, right?
we got false for both of them.
And oh, I'm sorry, I missed something here.
So if we try to instantiate the point objects twice and compare 0.
1 and 0.
2, we see that it is not equal, right?
So that's because if we don't have the, don't, equal to method, right?
Equality methods.
So if we define that like we did here, and we now instant, we try to compare them.
And we see that 0.
1 and 0.
2 methods.
now equal 3 are not equal.
So basically now we've been able to create, the fact that we created the
equality done the methods now means that when we use the double equal to,
it now works on our point objects, comparing X and Y values for equality.
So another.
Methods that we can create is the double underscore multiplication,
multiple collision, done the methods.
So like you see here, we added the multiplication methods.
And what this does is that when we try to multiply our class with, in
number, It brings out an actual output.
So we see that the multiplication operator now scales the point by multiplying it
our coordinates by the scalar value, which we passed here and three, which
is the scalar value we passed here.
We can also, yeah, several done the methods, but another
very interesting one is the.
let's implement this iterator method to make the point objects iterable.
So you can look through when we call the, when we implement this method,
what it does basically is that we can loop over like the coordinates.
So we can see how we, we did that here.
Okay.
but what happened is that now, when we try to loop over our, point objects, we make
them feel like native Python collections.
So now we can be able to loop through them.
Um, The next thing I'll be talking about is why Python Dundun methods
are really interesting to use.
we do not just want to use them everywhere in our code, right?
We do not want to abuse them.
So, this.
And, I think like we need to use them when necessary.
So they are really powerful.
And personally, I think like you should use them thoughtfully.
So you shouldn't like overuse them or misuse them.
And because they can make the code really confusing and unpredictable
or probably hard to maintain.
So there are several guides on where not to use Dunder method.
And, so for example, don't use them to solve like really simple problems.
So you can see here in our calculator class, right?
We just had, an add.
we have the Dunder method add.
To multiply, the, to multiply data, to multiply the values in the calculator.
So this is quite confusing because we shouldn't use that, if we are
using the ad, it should depicts adding values together, Another
thing too, is using the call.
like I said earlier, we don't want to, we don't want to use the, there's a
double underscore call method, right?
We don't want to implement that in our class because that's
quite unnecessary, right?
It's doing, it's, we might not really need it, right?
Or imagine implementing a call method that is actually adding numbers together.
So it doesn't make any sense.
So we want to use Dunder methods.
That depicts the right thing in your code.
So they don't become really confusing.
Also, you do not want to invent your own dunder methods.
Like I said earlier, if you declare a dunder method that does not exist
in Python native, in the native Python, it won't work, right?
It's just going to be like, you're going to be confusing everyone
because someone can hop on your code and see it and get confused because
this doesn't really do anything.
instead just use a regular method.
Also, operator, we don't want to make operator overload and really confusing.
like I said earlier, you don't want to just, use an add method to, Multiply
several, numbers, you don't want to use a multiply multiplication, done the
methods to add several numbers, right?
you want to keep it really consistent and very predictable.
So another thing is that you don't want to overuse it in your classes.
So you don't want to have a class that uses all the.
Done the methods just and makes your class like really confused.
You want to use them only when necessary.
I know that thing is that you don't want to sacrifice,
readability for cleverness, right?
So if you have a lot of this, done the methods, your class, it might
look cool, but it might be very difficult for others to maintain.
in conclusion,
using Dunder Methods It's really powerful, right?
So we've seen how we're able to combine Dunder methods to be able to build
the point class earlier and to make it look really rich and intuitive.
So these are like techniques that you can use to design objects that makes it feel
really natural in our applications domain.
For example, If you are working with a graphic library, you might want
to use something like the Dunder method underscore Dunder methods
add, or you're working like with a game development library, right?
It might make sense to use the equality Dunder methods for maybe
your collision detection, right?
So Dunder methods bridge the gap between your custom object and
Python's built in capabilities.
Allowing you to design APIs, an application that looks really
powerful, but also really easy to use.
Thank you.