1

I am learning python from Mark Lutz Books 5th Edition right now i am working out OOP in python. There is a concept of Operator Overloading. Okay i now very well what operator overloading and and how you use it to process your objects using e.g. arithmetic operators etc. But what is really driving me nuts is he is mentioning __init__ method which is basically a constructor in python as the representative of operator overloading class saying it as one of the methods belonging to the operator overloading family methods. Is it really something like this or is my perception wrong considering his lines and just how it is an Operator Overloading Method!?.Here is the reference to the topic.

2
  • You're overloading the constructor, when you define __init__ for a new class. Given your definition of operator, a constructor can be seen as an operator. Commented Jul 26, 2017 at 2:58
  • __init__ is a component of __call__, which is the () operator. Commented Jul 26, 2017 at 3:05

2 Answers 2

1

I think that __init__ is mentioned alongside the operator methods because it's a double-underscored special method like they are. But I think it's very misleading to call them all "operator overloading methods", since many special methods have nothing to do with operators.

I'd suggest reading the official Python documentation on this topic, where __foo__ type methods are just call "special methods". Some of them are indeed related to operators, but many others are related to converting between types (__str__, __bool__, __int__, etc.) or implementing some builtin functions (__len__, __iter__, __format__, __hash__, etc.). Others are usually only run indirectly, like the __get__, __set__ and __delete__ methods that implement the descriptor protocol. I'd group __init__ and __new__ in with those indirectly run methods, rather than with the ones that implement operators.

Sign up to request clarification or add additional context in comments.

Comments

0

My understanding of "Operator Overloading" is effectively that when you make a class, you can define the way that standard things work. For example, you could create a class where the addition operator (+) is used to concatenate items instead of finding their sum (i.e. 5+5==55 instead of 5+5==10). In this same way, when you define __init__, you're redefining, or 'overloading' the standard 'constructor' to make your own for that particular class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.