Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Erlend Dybvik Indrelid
5,843 PointsWhy do you even want to use magic methods?
Hi! I still can't seem to get why you should define the magic methods the same way they're already define. An example of this is in the python course where Kenneth defines the le like this:
def __le__(self, other):
return int(self) < other or int(self) == other
This doesn't make any sense to me, because you can already use the "<" and "==" operator without the definement. I understand the usage of it if you add more lines of code to it so it does something else when using the operator, but this piece of code is in my eyes just more lines of code that you don't need.
Can anyone explain me why you should define it? I also don't understand if python returns the values in the function when running through it, or if it needs to be called to return it's values.
Thank you for answering!
1 Answer
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI think the reason you might have to write your own custom method is if there was something about it that wasn't obvious or default. Like if I have a dog class, what does it mean for one dog to be <= another dog? Is it dog's length, weight, age, intelligence? In the body of the method you could access these properties or do your own custom logic.