0

I am learning python and inheritance. Are special methods - methods prefixed with __ inherited. i tried this code:

class a:
    def __str__(self):
        return "this is object of class a"
    pass
class b(a):
    pass

Printing object of a and b both give same output which proves this. Are __ prefixed methods also inherited than what is their difference from normal methods.

user2779311
  • 1,688
  • 4
  • 23
  • 31
  • explained here https://stackoverflow.com/questions/44444733/are-dunder-methods-inherited – deadvoid Oct 11 '18 at 17:44
  • Possible duplicate of [Are dunder methods inherited?](https://stackoverflow.com/questions/44444733/are-dunder-methods-inherited) – Austin Oct 11 '18 at 17:45

1 Answers1

2

Python's "magic methods", surrounded by double-underscores ("dunders") are essentially hooks into the object-oriented mechanisms of the language. See this post for detail on how they can be used.

To answer your question directly, magic methods are inherited like any other class methods and generally behave the same. The only difference is that they have special meaning to the Python interpreter, enabling programmer control over things like equality checking, iteration, context management, and a lot more.

Emmett Butler
  • 5,969
  • 2
  • 29
  • 47