Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dunder Methods
#1
I'm trying to get a basic understanding of dunder methods. Using the example below. How do I return the msg?
I'm thinking it just doesn't automatically pick the correct method then return the message.
Any help understanding would be great.

class Match:
    def __init__(self, guess, number):
        self.number = number
        self.guess = guess
        self.msg = ''

    def __lt__(self):
        if self.guess < self.number:
            self.msg = f'{self.guess} is too low'

    def __gt__(self):
        if self.guess > self.number:
            self.msg = f'{self.guess} is too high'

    def __eq__(self):
        if self.guess == self.number:
            self.msg = f'Great! You match the number with {self.guess}.'

    def __str__(self):
        return self.msg

print(Match(5, 3))
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Messages In This Thread
Dunder Methods - by menator01 - Mar-22-2022, 07:03 PM
RE: Dunder Methods - by ndc85430 - Mar-22-2022, 07:11 PM
RE: Dunder Methods - by menator01 - Mar-22-2022, 07:24 PM
RE: Dunder Methods - by deanhystad - Mar-22-2022, 07:56 PM
RE: Dunder Methods - by menator01 - Mar-22-2022, 08:32 PM
RE: Dunder Methods - by deanhystad - Mar-22-2022, 09:15 PM
RE: Dunder Methods - by snippsat - Mar-23-2022, 01:40 PM
RE: Dunder Methods - by menator01 - Mar-23-2022, 08:44 PM
RE: Dunder Methods - by deanhystad - Mar-24-2022, 03:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  a function name dunder Skaperen 3 1,097 May-29-2024, 06:13 PM
Last Post: Gribouillis
  Hi, looking for "dunder" tutorials. AlluminumFoil 3 2,908 Mar-02-2020, 08:37 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020