Magic Methods
Magic Methods
So by a super cool and awesome and really amazing developer who I work with sometimes, I have been set the task of trying to figure out how we would go about sorting two instances of the same class - based on the value of one of their attributes.
magic methods - taylor swift folklore aesthetic and vibe |
So what are magic methods?
Magic methods are "special methods describing how certain objects should behave."
They are "dunder methods" (sounds like something out of a fantasy novel?) like __init__, __repr__ and __lt__. I came across these when I was first learning Python/Django for my job interview.
These methods apparently indicate that these methods "shouldn't be called directly by the programmer, they are normally called by the interpreter itself." Source: https://blog.cambridgespark.com/magic-methods-a8d93dc55012
There is a thing called __new__ that comes before the __init__ method, but we are not going into this today.
I am not going to be going too much into __str__ and __repr__ today which are the two main methods for representing your classes because I have this task that I have been given and I want to focus on it so much.
Comparison methods
I wonder if comparison methods are my solution? Actually I know that I need the __lt__ method - I just don't know how to use it yet - is this a comparison method?
For context this is the class that I built today - so embarrassing - but it'll illustrate it at least. The Susanna can change on any given day - she can be programming in a different language, have a new favourite colour, and have a different amount of apples with her at work (I love fruits).
class Susanna:
def __init__(self, language: str, color: str, apples: int) -> None:
self.language = language
self.color = color
self.apples = apples
def __repr__ (self) -> str:
return f"Susanna id={id(self)}"
def __str__ (self) -> str:
return f"Today the Susanna is programming in {self.language},
likes the color {self.color}, and has {self.apples} apples with her
today. p.s. BANANA"
The task I have is to make sure that if I create two instances of the Susanna class, I can compare them by the number of apples.
So how do I do this - how?!
I was right! __lt__ is a comparison method. It defines the behaviour of the less-than > operator. But how do I use this then please - how???!
I got this far and then I realised I hadn't actually understood the assignment properly.
Was I meant to actually use the __lt__ method? Isn't it a magic method? What is it that it is actually trying to achieve?
THIS HAS WORKED
My code is an absolute shambles - I had to to it all again because my IDE deletes things when I click out - I need to use a better one.
But here we go - it worked. Based on the IDs, it looks like it returned the list in the opposite order than we wrote it in and that is what we wanted it to be - we wanted it to be in ascending order - I think. !!!
I'm not sure if this works for sure but I will check with my mentor tomorrow - thank you so so so much for reading.
Comments
Post a Comment