Dunder Methods

When making classes, I know about Python’s built in dunder methods. What if I make a unreal not built in Python dunder method for example:

class A: def __init__(self, letter): self.letter = letter def __letter__(self): return self.letter class B: pass class LetterError(Exception): def __str__(self, message): return message # A function for the dunder method __letter__: def letter(obj): try: return_letter = obj.__letter__ except AttributeError: raise LetterError(f"{obj.__name} class does not have a __letter__ dunder method.") else: return return_letter a = A("a") b = B() print(letter(a)) try: print(letter(b)) except AttributeError: raise LetterError(f"{obj.__name} class does not have a __letter__ dunder method.")

Would it just behave like a normal method?
Can you create your own dunder methods and create functions for them?
Why does this throw an error?

Yes, it just behaves like a normal method (so it should probably follow normal naming conventions). No you can’t really create dunder methods or at the very least you shouldn’t, just use regular methods and make the function to call them clever. As mentioned in the traceback, that object does not have some the attributes you are attempting to access.

2 Likes
class A: def __init__(self, letter): self.letter = letter def __letter__(self): return self.letter class B: pass class LetterError(Exception): def __str__(self, message): return message # A function for the dunder method __letter__: def letter(obj): try: return_letter = obj.__letter__ except AttributeError: raise LetterError(f"{obj.__name} class does not have a __letter__ dunder method.") else: return return_letter a = A("a") b = B() print(letter(a)) try: print(letter(b)) except AttributeError: raise LetterError(f"{obj.__name} class does not have a __letter__ dunder method.")

How do I properly implement the __str__ method?
Is migth me good to pretend the __letter__ method as a dunder method because othere classes could have the letter method. I would call __letter__ a pretended magic method that is used to show the difference between letter and __letter__. Why was there an error even though I caught errors using exception handiling?

Come on man, that seems like a great thing to ask your search engine, put a little effort in.

You can, you’re just advised to follow the naming conventions when you do it.

It seems that you didn’t catch them all or simply re-raised them.

1 Like

What about this:

class A: def __init__(self, letter): self.letter = letter def __letter__(self): return self.letter class B: pass class LetterError(Exception): def __str__(self, message): return str(message) # A function for the dunder method __letter__: def letter(obj): try: return_letter = obj.__letter__ except AttributeError: raise LetterError(f"{obj.__name} class does not have a __letter__ dunder method.") else: return return_letter a = A("a") b = B() print(letter(a)) try: print(letter(b)) except AttributeError: raise LetterError(f"{obj.__name} class does not have a __letter__ dunder method.")

.
I think self defined dunder methods might be useful maybe in AI. Pretended you wanted a walk() function for each robots and also other machines. You should use a dunder method like __walk__() to be able to define different behaviours of custom machines more easier. Use could then define a function like this to make it look like some real dunder method and make it easier to call the dunder method:

def walk(machine, speed): machine.__walk__(speed)

Then you can be able to use different behaviours for walk() by defining the self defined dunder method! So that is why I think that self made dunder methods can be useful. Do you agree?