hi @monorail would you like to see a really fucking stupid joke

@The_T @monorail its a programming joke so i wasnt sure you would get it

the mechanism available to Python programmers to modify initialization logic for certain kinds of data called "objects" (don't ask) requires creating a function called __init__

@The_T @monorail most other languages called this function a "constructor"

⚠️ i've been writing python for most of my time on earth ⚠️ 

@wallhackio @The_T extremely loud incorrect buzzer

most languages don't have anything exactly equivalent to __init__, but they have something equivalent to python's __new__. in python we just prefer to create bare objects and initialize them rather than do it in the constructor for some reason

re: ⚠️ i've been writing python for most of my time on earth ⚠️ 

@monorail @wallhackio @The_T I'm an absolute noob at Python and mostly use it for non-object-oriented scripting, can you tell me about the difference between __new__ and __init__?

Follow

re: ⚠️ i've been writing python for most of my time on earth ⚠️ 

@vaporeon_ @monorail @The_T normally in OOP, object instantiation logic is contained within a constructor, but Python decided to split the process into two steps:

1) __new__ is a function which returns the instantiated object,
2) __init__ is a function that receives the instantiated object and and initializes its state

It is very rare that a Python programmer ever use __new__. In 99% of cases, what you would put in a constructor in some other language is put in the __init__ method when you use Python.

You can override __new__, however, so that it returns anything you want. You typically do this when you subclass immutable types. Because, if you try to manipulate a subclassed immutable object in the __init__ function, well you can't. Because its immutable. So you have to override the creation of the object itself, typically by calling the superclass's constructor, in the __new__ method.

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.