THIS Will Give You MORE Flexibility With Your Python Classes

preview_player
Показать описание
This will give you more flexibility with your Python classes #code #python #programming
Рекомендации по теме
Комментарии
Автор

tip: don't name your methods "meth"

mosomiliardaru
Автор

Name it simply 'method' ✖
Name it a drug ✅

eidnoxon
Автор

You actually shouldnt return just a number. People using your code (or yourself in 5 months) expect an operation like this to return the same type (or maybe something equivalent for vector math for example). You could for example add the names and costs together and return a new Expense like that.

mathijsfrank
Автор

The problem is.. I can't do Expanse + Expanse + Expanse... because __add__ return a number instead of a instance of Expanse..

walkdead
Автор

KInd of a confusing use of ___add___ since you expect it to return a class of the same type as the two you added. I wouldn't recommend this.

vorpal
Автор

Where possible you should return an object of the same class type when doing this. The example breaks if 3 or 4 expenses are added. A better approach would be to return self.__class__('Total', self.cost + other.cost)

This will allow you to have food, fuel, lodging, and tolls and do:

total = food + fuel + lodging + tolls

Where the original will give an error unless you do something weird trick like

total = (food + fuel) + (lodging + tolls)

And that is not clear what is happening and only works with pairs of Expenses. If one only has an odd number, you have some really ugly thing like:

total = food + fuel + lodging.cost

BillBrandt
Автор

Note: “Dunder method” means double underscore method. They are magic methods defined by built-in classes in Python and commonly used for operator overloading.

Jschmuck
Автор

This is useful for classes, but you also have to look out when the other is not Expense
In such case you get AttributeError (like str has no attribute cost) or return invalid info if it has
The solution is to check ifinstance then return (nope, not raise) a NotImplemented
(which might be a good for a short)

ProfRoxas
Автор

Shouldn't the sum of two expenses be another expense?

__christopher__
Автор

I think __add__ should return the same type as its two parameters. Anything else would be unexpected.

blenderpanzi
Автор

"Adding" two things should return another thing of that type. Otherwise you e. g. can't chain additions together. This is not what I would expect addition to behave like.
You could change the Expense name to be a list or set of names, and then have __add__ return a new Expense who contains the joint list or set of names, and whose cost is the sum of the partial costs.

benhbr
Автор

Thank you for the video. Can we consider this as operator overloading?

theilich
Автор

It looks like operator overloading in C++.

simaobonvalot
Автор

Would have made much more sense to return a new expense that also combines the names

Einstine
Автор

I swear, between this and TikTok, I'm convinced I don't know how to tie my shoes.

c.michaelfisher
Автор

I'd rather have the semantics of the + be narrow. Reading ```a.cost + b.cost``` is gonna cause confusion to noone as with ```a + b``` you have to remember that they are of a type you overloaded the + operator for and what logic you implemented to get to the answer.

bonenintomatensaus
Автор

I'm following a tutorial to make a Pac-man game that had this exact function, but I couldn't figure out what its for, even when playing with the class lol

This helped clear it up

ciberkid
Автор

Generally speaking, an overloaded binary operator is expected to return a new instance of the same class, so in this case it should return a new instance of Expense. However, it would be difficult to determine what the value of the _name_ in the new instance should be. Thus, I would say this is not a good example.

Salvage
Автор

I would advise to always return the a new instance of the input type from operator overloads if possible, i.e. to make __add__ return a new Expense()
Overloading can get very confusing very quickly, but doing it like this will at least soften the effect.

SkyyySi
Автор

For those who have questions if two instance of a class can be added.
I think yes.

But here probably just want to add the cost.

But I guess you can add other than numbers.

asagiai