Operator overloading - Intermediate Python Programming p.19

preview_player
Показать описание
Welcome to part 19 of the intermediate Python programming tutorial series. In this tutorial, we are going to introduce the "special" or "magic" methods of Python, and, with that, talk about operator overloading, which is where we can define new ways for Python to handle operators like "+" in our example.

Рекомендации по теме
Комментарии
Автор

Basically, without resorting to C, one can create your own datatypes using operator overloading. I've done this myself when I had a project that needed ordered-named-lists (like a dictionary, but the keys need not be unique.) Wish your videos had been around when I was learning how. Some extra info for others: the example he used is good for blue_blob + red_blob. But red_blob + blue_blob will fail; that is because the __add__ operator is referenced by the first object, with the second object as the parameter. One could need to define __add__ in the RedBlob class for __add_ to become "commutative" (i.e. getting it to work in both directions.) Or, one could add a default __add__ to the Blob parent class; then overload again for unique properties based on color. ...it is easy to go down some twisty rabbit holes with these techniques... :)

JohnDupuyCOMO
Автор

Great video, you explained the basic very well.
Two things I find need to be mentioned. First of all: you can add / subtract and so on different objects. One thing that you need to keep in mind though is that a + b is basically a.__add__(b) the addition is not commutative.
The other thing is that like you showed in the video the addition doesn't have any return value so for example bloby = red_blob + blue_blob. Then bloby is None. If you want to have a return value, create a new object.

mamazu
Автор

This is like watching a Bob Ross video. Thanks!

fennecfox
Автор

I think another way to think about operation overloading is to draw from concepts of a 'space' in linear algebra. From what I remember, when a new space or something like a sub-space is created, your space is empty at first, but then you need to define what types of operations exist in that space.

Dgrayfit
Автор

and as always thank you for changing my life

roshanmehta
Автор

Can you do a video to make or explain the first level or "metal slug" or "street of rage " in python

tcgvsocg
Автор

awesome video, very useful and clear!

cmatthew
Автор

What does "blue_blobs[0] + red_blobs[0]" at 8:44 mean by itself ??? Is it to make 2 blobs collide ? Can anyone explain a bit more please.... some id + some id don't sound like a valid command to me.

turboromy
Автор

There is a mistake in the written part of this tutorial in the last code snippet. There is a parenthesis missing at the end of the second print because of the additional str() method, which isn't in the video.

MyvilMe
Автор

you are fucking legend, thanks for the great content I'm gonna donate now

dogugerger
Автор

Any reason why you don't say "other_blob == RED/GREEN/BLUE" in the BlueBlob class, i.e., using your defined color tuple variables?

Also, nice Osmos clone ;)

iNuchalHead
Автор

winds me up all that code means nothing if you don't show the result of it! it looks like all that code just makes lil squares hmmmm strange.

visit shbcf.ru