__new__ vs __init__ in Python

preview_player
Показать описание
What's __new__ and what's the difference vs. __init__?
Use coupon code MCODING at checkout for up to 91% off all yearly hosting plans!

What's the difference between the "__new__" and "__init__" magic methods in Python? New is for object creation, and init is for object initialization. It is rare that a programmer would actually need to override new, so let's see some examples of how it can be used.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Laura M, Jameson, John Martin, Dragos C, Vahnekie, Pieter G, Sigmanificient, Casey G

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:50 New vs init
2:51 Main purpose, subclassing immutable types
5:03 Singleton example
6:35 Client caching example
7:58 Encrypted file example
10:33 Metaclasses
Рекомендации по теме
Комментарии
Автор

This chanel has a very high python value. It's nothing like the others, recycling docummentation or from one another. You can easilly see this on the file examples, where there are codecs and itertools correctly used.

JakubYTb
Автор

Decrypted message zoom in was priceless.
As usual, excellent video.

NOPerative
Автор

I really like how you get straight to the point and give clear and concise explanations. This channel is one of the best if not the the best resource for understanding more difficult concepts in python.

kennethlew
Автор

Quality content, yet again. Idiomatic Python, concise explanations, nothing less. 11/10

azratosh
Автор

I didn't even know __new__ was a thing until seeing this video. I've always used __init__ and that's what I've seen other people always do as well.

Amechaniaa
Автор

Nice video! That last point really highlighted where I might personally use __new__ in my own projects, really insightful video!

micalobia
Автор

This channel is the only one on YouTube that actually showcases unique and interesting stuff and doesn't just read out the iter-tools docs.

royler
Автор

The value of a singleton is when you need an abstraction to something where there is an actual constraint that "there can be only one". The classic example is hardware access. Suppose you open a connection to a device that only supports a single connection (e.g. a COM port), and a set of objects that can access that device. Making the device abstraction a singleton is a great design pattern. Any object can instantiate the device object. If it's the first one, a new object is instantiated. If it's a subsequent construction, the object gets a reference to the already instantiated device.

plfreeman
Автор

Man your videos are awesome. I like that they are so much on point without artifical fluff to make them unnecessarily longer. Every video has one topic and sticks to it similar to "a function should do one job only" :). Great job!

robertbrummayer
Автор

I love those videos where you look at one single issue and explain it in-depth! while all of those videos may already exist on yt, most are just not as well made. yours are a joy to watch!

georgplaz
Автор

I spent hours on internet, this is the only video/blog explain clearly about __new__ and __ini__ !! Especially the part about when we need to modify __new__(). Thank you so much!!

haoli
Автор

Another banger, as we've come to expect at this point :)
I remember reading the documentation on this but the only thing that stuck was the fact new creates the class and runs before init - what that actually meant or what I could do with it I had no idea, but changed this! Great video

oxey_
Автор

Hi, I really like your videos and they always help me understand something i didn't know in python! Thank you!

yonataneavri
Автор

Holy shit, although I work everyday with python on an advanced level your videos make me feel like I didn't know anything. I really like your videos since it seems like they are the only resource constantly delivering pro level python tutorials.

comedyclub
Автор

Brilliant video. You are one of the very few programming channels which are presenting stuff which helps you move from a beginner software developer, to an intermediate one.

prakhargarhwal
Автор

Had I not seen this last year, today I would have fumbled around with __init__, not understanding what I was doing, and it would have cost me hours. Cheers for another top shelf lesson in the finer points of Python

joewyndham
Автор

Man I'm just so happy that such high quality content is for free. Greetings from Germany!

christiansinger
Автор

Excellent, just as always! The Python level you teach is just another level. Love your videos.
Can you do a tutorial on super function as well? I would really appreciate that.

qorbanimaq
Автор

oh man - the awkward pause to subscribe was GOLD.
these vids are great - I didn't know about the __new__ method - I'm intrigued!
Sincerely,
-new subscriber

marquinhop
Автор

I always wondered why the heck we use 'self' in the __init__ method. Now I know! Thanks for this informative video, it helped alot!

punktdotcom