On Tue, Aug 10, 2021 at 8:19 AM Mats Wichmann <m...@wichmann.us> wrote: > Even if you do > > x = 2 + 3 > > you're actually creating an integer object with a value of 2, and > calling its add method to add the integer object with the value of 3 to > it. The syntax hides it, but in a way it's just convenience that it does > so... > > >>> 2 + 3 > 5 > >>> x = 2 > >>> x.__add__(3) > 5 > > > sorry for nitpicking :) But... don't be afraid of letting them know > it's OOP, and it''s not huge and complex and scary! >
Since we're nitpicking already, "2 + 3" isn't the same as "(2).__add__(3)"; among other things, it's able to call (3).__radd__(2) instead. Plus there's technicalities about type slots and such. If you want to highlight the OOP nature of Python, rather than looking at magic methods, I'd first look at polymorphism. You can add a pair of integers; you can add a pair of tuples; you can add a pair of strings. Each one logically adds two things together and gives a result, and they're all spelled the exact same way. Dunder methods are a way for custom classes to slot into that same polymorphism, but the polymorphism exists first and the dunders come later. ChrisA -- https://mail.python.org/mailman/listinfo/python-list