'__new__ VS __init__' In Python Tutorial (Simplified Explantion)

preview_player
Показать описание
In this video we're going to be looking at the difference between __new__ and __init__ in Python! We'll cover how they work and how we can use them.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Intro
00:20 Getting started
00:49 Private attribute
00:58 __new__
02:53 __init__
03:03 Running the example
04:27 Quick summary
05:02 Another example
08.28 Summing it up
Рекомендации по теме
Комментарии
Автор

I think it’s worth noting that in the singleton pattern example, “init” will *always* get called after “new” if an instance is returned in “new”, meaning that if constructor gets called twice and you are setting up some attributes in “init”, those will get overridden in the next call, but object instance will be still technically the same in the example of the video.

Darktega
Автор

Concise and clear as always I'm finding. Thanks for some excellent explanations.

BrianSwatton
Автор

new day new information... This is the first time i see the __new__ keyword in Python. Thanks Indently, subscribed

berkaybakacak
Автор

Good video. I have almost never used __new__ and have done my singletons through metaclasses where a Singleton metaclass maintains a dict of instances, but this did actually teach me some things I wasn't entirely sure of about __new__.

vorpal
Автор

The way to look at __new__ is as a way to "intercept" the instance creation process.
The class can then behave differently depending on things like which OS you're running, the params you passed, whether an instance of a class already exists (singleton pattern), whether certain data is already cached, etc.

For example, pathlib, a popular package to deal with OS paths, has two different implementations of the Path object. One for Windows, and one for Posix systems (like Mac or Linux).
When you create a Path object, it will perform a check to see which OS it's running on, and instantiate the appropriate subclass.

Note that this is different than a factory pattern, where you have to pass some parameter to know which object you want the factory to spit out.
With __new__, you get a unified interface that can abstract away that for you.

chrism
Автор

Been lovong your channel. Lots of neat insight!

thodorisevangelakos
Автор

This is what I think is happening from the Vehicle example. If a vehicle has either 2 or 4 wheels, they become instances of the Vehicle class that has been initialized beforehand. But in a rare occurrence where a vehicle does NOT have 2 or 4 wheels, a new class is automatically created using __new__, but you will also need to initialize this new class with the necessary parameters/arguments. In a nutshell, you use __new__ as some kind of a "backup" class in case the instance does not meet the originalclass parameters.

purplecrayon
Автор

The thing I found out is that you can annotate stated types in __new__ and dynamicly annotate in __init__ with TypeVars for Generics as an example. Just to not mess up with code.

YourCloseCoop
Автор

Great video. Do you have any video explaining why ___ is used to initialize variables? I did it in C#, but I don't know if it's the same thing.

luis_english-xygh
Автор

Usually only used in library codes, avoid using __new__ at work. unless you have no other choice. Your IDE will get confused by this as it does not execute __new__ for u and it just assumes constructing an instance will return a new instance. Not to mention people that use your class would get so confused and could make mistake.

NoProblem
Автор

3:56 if the object doesn't have __eq__ the code performs de is operator

davidm.bm
Автор

Can someone tell me how was Super() invoked even though Connection isn't extending any parent class?

yashkulkarni
Автор

I been thinking this dude lately does videos just to get something out with little to no value, yet this one is quite interesting new thing that I didn't know of, and might be helpfull in some occasions, I would definetly try it out...hope it's now a feature of python 3.11 cause I am using 9 and 10

Sinke_
Автор

Unfortunately, this did not really explain to me what __ new__ actually does. It was more of a complicated Singleton Implementation

turboblitz
Автор

I think this is like instead of creating builder functionality

seanshimon
Автор

So in the last example, when we create mb = Vehicle(2), mb is an object of the class Motorbike and no longer Vehicle?

maxca
Автор

how on earth do you get pycharm to look that good

arkie
Автор

Great video, but it's pretty much a complete copy of mCoding. In his video he has the exact same connection example, and a similar example to vehicles.

alexjando
Автор

And that's how you implement a Singleton using Python.

CoentraDZ
visit shbcf.ru