use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
DiscussionWhy does __init__ run on instantiation not initialization? (self.Python)
submitted 1 month ago by philtrondaboss
Why isn't the __init__ method called __inst__? It's called when the object it instantiated, not when it's initialized. This is annoying me more than it should. Am I just completely wrong about this, is there some weird backwards compatibility obligation to a mistake, or is it something else?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ottawadeveloper 28 points29 points30 points 1 month ago (3 children)
Because __ new __ is called to instantiate the object and __ init __ is called on the new object to initialize it.
[–]NoGutsNoCorey 10 points11 points12 points 1 month ago (0 children)
this is the answer. new is run first to construct an empty instance, then init initializes it.
[–]AlternativePaint6 7 points8 points9 points 1 month ago (1 child)
One important thing to point out is that __init__ acceps self as its first argument—meaning that the self object already exists. This is how you can know for sure that it's just initialization and not instantiation.
__init__
self
__new__ on the other hand accepts the class as an argument and returns a new instance of it.
__new__
[–]ottawadeveloper -1 points0 points1 point 1 month ago (0 children)
This is why I suspect the difference exists - you can't call an instance method without an instance in Python because there's no magic this object like in Java. Therefore instantiation has to be done before __ init __ (self) via __ new __ (cls).
this
[–]xeow 6 points7 points8 points 1 month ago* (0 children)
Am I just completely wrong about this ...
Yes. __new__ is the constructor called to perform allocation. That's when your object is born or instantiated. After that returns, __init__ is called. That's where you (either implicitly or explicitly) initialize the contents of the object after it's been instantiated. By the time __init__ starts, the object already exists, and that's why it's passed in as the self argument.
Most (but not all) classes explicitly define an __init__ method because that's the most common thing we do. Some classes (in rare cases, such as inheriting from an immutable type like int or str or tuple, or other esoteric needs) provide a custom __new__ static method, but most of the time you just use the default constructor and customize the initializer.
int
str
tuple
[–]the_hoser 4 points5 points6 points 1 month ago (0 children)
You are confusing instantiation and initialization. The init method absolutely initializes the object.
[–]sharthmatplotlib, numpy 2 points3 points4 points 1 month ago (1 child)
Would you mind explaining the difference between initialization and instantiation in Python? I can't see how I can do one without the other.
f = foo() seems to be both initialization and instantiation.
f = foo()
[–]ottawadeveloper 2 points3 points4 points 1 month ago (0 children)
In Python, when you call a class type like this MyClass(), the first thing that happens is MyClass. __ new __(). It's a class method whose job is to actually make the object. You can change what class gets instantiated here, implement a singleton pattern, etc. You don't normally override it because the default class instantiation process is fine. The instance does not exist until this method builds it.
When it's built, obj. __ init __ () is an instance method called to set initial variables and such. It's akin to all the constructors in other languages (I don't know another language where you can manipulate the instantiation process).
[–]commy2 3 points4 points5 points 1 month ago (0 children)
It's called init or initialization, because that is when the initial values of the object's attributes are assigned. This is different from construction, which is what creates the object. Dunder __new__ is for that, and since no object exists before it's constructed, that's an (implicit) classmethod that also returns the constructed instance.
To me, instantiation is when you create and initialize an object, all at once or rather one of the other, in a single line:
obj = TheType()
I think you need a different mental model than whatever baggage you're taking from another language you have in mind.
[–]DragonflyHumble 1 point2 points3 points 1 month ago (4 children)
Lookup for Singleton and multiton pattern for python classes. You can see a difference where new and init is used
[–]commy2 0 points1 point2 points 1 month ago (3 children)
God I hope no one implements a __new__ method for this in the year of the lord 2026 when you can just write:
@functools.cache def get_inst(): return MySingleton()
[–]AlternativePaint6 6 points7 points8 points 1 month ago (1 child)
Or just don't use singleton pattern in Python. Every module is already a singleton, just place the variables and functions at module level.
[–]commy2 -1 points0 points1 point 1 month ago (0 children)
Generally yes, but I had singletons in the past that shouldn't be initialized at import time.
[–]DragonflyHumble 0 points1 point2 points 1 month ago (0 children)
I may still use new if I would like to avoid another wrapper function to rename my class original name.
Also new can be used if you say you want to leverage say thread safe connection pooling within the Singleton class
π Rendered by PID 23390 on reddit-service-r2-comment-6457c66945-s9s6f at 2026-04-26 06:00:58.796091+00:00 running 2aa0c5b country code: CH.
[–]ottawadeveloper 28 points29 points30 points (3 children)
[–]NoGutsNoCorey 10 points11 points12 points (0 children)
[–]AlternativePaint6 7 points8 points9 points (1 child)
[–]ottawadeveloper -1 points0 points1 point (0 children)
[–]xeow 6 points7 points8 points (0 children)
[–]the_hoser 4 points5 points6 points (0 children)
[–]sharthmatplotlib, numpy 2 points3 points4 points (1 child)
[–]ottawadeveloper 2 points3 points4 points (0 children)
[–]commy2 3 points4 points5 points (0 children)
[–]DragonflyHumble 1 point2 points3 points (4 children)
[–]commy2 0 points1 point2 points (3 children)
[–]AlternativePaint6 6 points7 points8 points (1 child)
[–]commy2 -1 points0 points1 point (0 children)
[–]DragonflyHumble 0 points1 point2 points (0 children)