r/Python 3d ago

Discussion Constructors: __init__, __new__, both, neither?

Hi all, I'm doing some research on what programmers believe is the class constructor in Python. I made a poll here: https://strawpoll.com/05ZdzVzdmn6 and would appreciate all responses, thanks!

0 Upvotes

22 comments sorted by

View all comments

2

u/pbecotte 3d ago

Considering the python object model, "constructor" would be very ambiguous defined. I could see a legitimate argument for all four answers. Unless you're just trying to get an idea of what percentage of your respondents actually understand the object model?

1

u/declspecl 3d ago

Exactly, I think a case could be made for each one. I'm really just trying to see what the distribution is of what people thing of when they think "constructor"

Though personally I would probably vote for `__init__` , since constructors in other languages tend to be initializers and not the actual allocating function. Although Python is tricky because of when fields are defined and initialized. But in the same breath, "fields" don't exist in C++ like they do in Python or Java, and I'm positive almost nobody would argue that `malloc` is a constructor. So definitely an interesting discussion imo!

2

u/pbecotte 3d ago

If we are having a philosophical one, I think I'd go with "both" since you generally need the combination to actually build a useful object- and don't understand the object model of any other language well enough to actually know the difference lol.