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/ToddBradley 2d ago

I came to Python by way of Java and C#, and have been using it weekly for the past several years. I have never once used __new__, only __init__. And of code I've maintained that was written by others on a few different projects, I haven't seen a single person use __new__.

So I voted __init__, because for the colloquial use of the word "constructor", that's what it seems like the vast majority of people use.

1

u/yrubooingmeimryte 1d ago

But you can create classes in python without ever writing an init method. So if they are optional, can they really be considered the constructor of the object?

1

u/ToddBradley 1d ago

Beats me. Every language I've used in the past 20 years that has constructors also makes them optional - Java, C#, and Python. C++ didn't have the concept of a default constructor when I used it in the 90s, but maybe that's changed since then.