New in Python 3.13: Replace

  Рет қаралды 50,985

Indently

Indently

Күн бұрын

Пікірлер: 79
@michaelhoffmann2891
@michaelhoffmann2891 2 ай бұрын
The fact that it's shallow is something that I just *KNOW* will come to bite me. Generally when the caffeine has worn off and the brain is going into power-saving mode.
@prepsure_
@prepsure_ 2 ай бұрын
i think the idea here is that your classes are immutable. replace only really useful in this case, it’s a functional construct, so shallow copying shouldn’t be an issue because the data won’t be mutable anyway
@XxZeldaxXXxLinkxX
@XxZeldaxXXxLinkxX 2 ай бұрын
​@@prepsure_yep you needed to have fucked up twice already if you're running into data consistency problems using immutable type classes 😂
@Libertarian1208
@Libertarian1208 2 ай бұрын
There is a method dataclasses.replace (in Python 3.12 at least) - for dataclasses copy.replace is not needed
@martinpayne2934
@martinpayne2934 2 ай бұрын
I think it has been around for as long as dataclasses have existed. It definitely exists in 3.10 anyway. But perhaps this more generalised version of replace is intended to replace (pun intended) the one in the dataclasses module.
@fyellin
@fyellin 2 ай бұрын
There is also the method NamedTuple._replace. Does this new method do anything that can’t already be done?
@rustyguard1001
@rustyguard1001 2 ай бұрын
​@@fyellinit bacically unifies all the different replace methods with introduction of __replace__
@PeterZaitcev
@PeterZaitcev 2 ай бұрын
​@@martinpayne2934 since 3.7
@pharoah327
@pharoah327 2 ай бұрын
This is essentially the "with" keyword in C#. Glad to see Python getting this feature!
@ZelenoJabko
@ZelenoJabko 2 ай бұрын
c# stole this feature from Scala.
@pharoah327
@pharoah327 2 ай бұрын
@ZelenoJabko languages "steal" from other languages all the time. It's nothing new. It's actually a very positive and healthy thing. Again, I'm glad to see Python "stealing" this feature as I think it can be very useful. I know I used C#'s version of it recently in a project and it was great.
@snesmocha
@snesmocha 2 ай бұрын
@@ZelenoJabkoevery language steals from lisp 💀
@soniclettuce
@soniclettuce 2 ай бұрын
@@ZelenoJabko and Scala stole it from OCaml
@ecaltroyer
@ecaltroyer 2 ай бұрын
The KZbin algorithm has got you
@krol_zeliwko
@krol_zeliwko 2 ай бұрын
"what am I even saying" 😂😂😂
@oida10000
@oida10000 Ай бұрын
Is there an equivalent method for replace with deepcopy? Maybe deepreplace?
@rusca8
@rusca8 2 ай бұрын
I mean. The dataclasses module itself already has a replace function that works like that.
@ezkymos
@ezkymos 2 ай бұрын
What is the difference compared to using cup.copy(price=100) ?
@m4rcika
@m4rcika 2 ай бұрын
Really clear and useful, thank you
@23nine
@23nine 2 ай бұрын
I didn't even know about dataclasses. Thanks.
@seanshimon
@seanshimon 2 ай бұрын
You must
@samuelpinzon8329
@samuelpinzon8329 2 ай бұрын
Python 3.13 has some good features, and I wanna learn it all!! But I still don't know most of the features of the past versions 😭😭😭😭
@ludovicbossard9577
@ludovicbossard9577 Ай бұрын
We all have to start somewhere
@Imperial_Squid
@Imperial_Squid Ай бұрын
No coder knows every single feature of the languages they use, unless you're one of the people making it, it's not necessary.
@notanerd3141
@notanerd3141 2 ай бұрын
3:08 why use replace when you can just make a new instance?????
@joshix833
@joshix833 2 ай бұрын
It would be more useful if the class had many you want to keep the same
@buycraft911miner2
@buycraft911miner2 2 ай бұрын
its super useful if you decide everything to be inmutable​, which is common in functional programming
@stunnerr
@stunnerr 2 ай бұрын
That's just an example, real dataclass could have more fields than that
@seanshimon
@seanshimon 2 ай бұрын
Can you please show some Pytest examples and features, please.
@davidgillies620
@davidgillies620 2 ай бұрын
I want to use 3.13 but too many packages have dependency issues still (I use miniconda for data wranglingstuff).
@sohangchopra6478
@sohangchopra6478 2 ай бұрын
Nice feature!
@adobko
@adobko 2 ай бұрын
Amazing as always
@immortalsun
@immortalsun 2 ай бұрын
Thanks for the video :)
@sectorgamma
@sectorgamma 2 ай бұрын
So why isn't there a deep_replace the same way there is a deep_copy?
@joeldick6871
@joeldick6871 Ай бұрын
How about deepreplace?
@shafqatjanjua4879
@shafqatjanjua4879 2 ай бұрын
Keep going... [for other whatever topics]
@Dulinniel
@Dulinniel 2 ай бұрын
Genuinely, why would you change the value of an immutable object in a first place ? I mean, isn't there any dynamic data structure in python ?
@JNSchneider
@JNSchneider 2 ай бұрын
This is not for changing values of immutable objects, if you want a mutable dataclass for instance, you just don't set frozen to true. The use case for this method is when you want to create a second immutable object that is similar to the first but slightly different. Since the object is immutable, you cannot make a copy and change it. Replace helps you create a copy that is different in the keys that you specify but still the same in all other properties and also immutable.
@foxypiratecove37350
@foxypiratecove37350 2 ай бұрын
That's actually good.
@e-pluszak9419
@e-pluszak9419 2 ай бұрын
Price of type float, ouch 😳
@Indently
@Indently 2 ай бұрын
If your country doesn’t have coins you can always use an integer.
@e-pluszak9419
@e-pluszak9419 2 ай бұрын
@Indently yes, but if it does using floats is still insane due to rounding errors, Decimal class is the way to go
@yhtomit28
@yhtomit28 2 ай бұрын
​@@e-pluszak9419 or another common trick with currency is to store it in terms of the smallest denomination and you always have a integer, e.g. for USD store an integer number of cents, or for GBP store an integer number of pence.
@lemonteurdesanuseur9686
@lemonteurdesanuseur9686 2 ай бұрын
Anyone knows in what case this might be useful ?
@eternlyytc7300
@eternlyytc7300 2 ай бұрын
Thanks
@ItsJoeyG
@ItsJoeyG 2 ай бұрын
this is HUGE
@Kynatosh
@Kynatosh 2 ай бұрын
I don't use python much but from those examples I don't see the difference of using NamedTuple and dataclasses, what's the difference? Edit ah dataclasses are mutable by default, frozen=true makes them immutable. What's the difference then
@DuncanBooth
@DuncanBooth 2 ай бұрын
The fields in a named tuple may be accessed either by name or by index. Before named tuples existed functions such as os.stat() simply returned a ten element tuple. It still returns a ten element tuple for compatibility with old code but these days you wouldn't think of using anything but the named fields. Dataclasses are simply classes so they don't have any of the tuple baggage.
@Kynatosh
@Kynatosh 2 ай бұрын
@@DuncanBooth So why would anyone use Frozen dataclasses if they are less permissive? I assume NamedTuples are faster too because c implementation and not dictionnary Edit: Actually, Maybe for typing it's good, because tuple NamedTuple1(x=1, y=2) == NamedTuple2(x=1, y=2) But Dataclass1(x=1, y=2) and Dataclass2(x=1, y=2) are different
@xbylina2641
@xbylina2641 2 ай бұрын
@@Kynatosh "Less permissive" doesn't mean "less usable" -- tuples are less permissive than lists and both have their uses.
@juanjosefarina
@juanjosefarina 2 ай бұрын
@@Kynatosh Mutability may result in ugly bugs, and using less permissive structures is recommended for a similar reason. You want to make as restricted and deterministic as possible your code.
@EndermanAPM
@EndermanAPM 2 ай бұрын
I like the feature, but don't really love the name. "replace" means replace, not "copy and replace". At least in my head.
@rubynaxela8524
@rubynaxela8524 2 ай бұрын
I've already answered another person who also didn't like the name, so I'll just copy my opinion here. A name like replaced() would be probably more consistent with functions like sorted() or reversed(). However that name could also suggest that we're somehow replacing the object itself, and not values of some of its fields, so perhaps a name like with_replaced() (or copy_and_replace()) would be even more transparent. But I think that at this point everyone has learned that Python functions (global or static, not instance methods!!), by convention, never modify their arguments and return modified, new objects instead, so there's probably no need for verbose names.
@chri-k
@chri-k 2 ай бұрын
i would have called it new_from. but i also don't know anything about Python naming conventions.
@josejaimecome
@josejaimecome 2 ай бұрын
Clear and Concise, that is why I always keep my self up to date with the new python release. But I try to not forget the old one as most of the code will be in old version.
@el_chivo99
@el_chivo99 Ай бұрын
copy should be called shallowcopy and replace should have been some argument of shallowcopy, like a dictionary.
@WondrousHello
@WondrousHello 2 ай бұрын
I don’t understand what the real world use case for this would be
@sseymour1978
@sseymour1978 Ай бұрын
python 14 : deepreplace :D
@SarcTiann
@SarcTiann 2 ай бұрын
You might want to write `golden_cup = replace(deepcopy(cup), cost=100)` Or `... deepcopy(replace( ...`
@MrKerim2000
@MrKerim2000 2 ай бұрын
Should be called replaced instead of replace imo
@Omsip123
@Omsip123 2 ай бұрын
Hmmm, and copy would be copied? I think I see your point (which you did not explain), but I think replace is just fine as well.
@rubynaxela8524
@rubynaxela8524 2 ай бұрын
Yeah, I get it, it would be probably more consistent with functions like sorted() or reversed(). However that name could also suggest that we're somehow replacing the object itself, and not values of some of its fields, so perhaps a name like with_replaced() would be even more transparent. But I think that at this point everyone has learned that Python functions (global or static, not instance methods!!), by convention, never modify their arguments and return modified, new objects instead.
@pharoah327
@pharoah327 2 ай бұрын
​@@rubynaxela8524always remember that new people are joining the field all the time and are learning Python for the first time. Consistency in naming is very important. Old hats will adapt but newbies will still be confused. Unfortunately I don't think Python language developers put near enough time into thinking about names and the inconsistency shows. So the distinction between replace and replaced or with_replaced is an important discussion that Python lang devs should have had.
@osamasrour4757
@osamasrour4757 2 ай бұрын
❤❤❤❤
@aømid-GT
@aømid-GT 2 ай бұрын
Is it weird that I Watch these Python vids even tho I use Lua?
@outofmemind
@outofmemind 2 ай бұрын
No, I mainly use rust
@MynameisBrianZX
@MynameisBrianZX 2 ай бұрын
nah I am not in a profession nor do I ever desire to casually use a statically typed language, but I still watch videos about them because peeks at language design is still interesting. People love tourism shows even if they’ll never travel.
@MichaelWoodrum
@MichaelWoodrum 2 ай бұрын
local myAnswer = setmetatable({}, { __tostring = function() return "yes" end }) print(tostring(myAnswer))
@outofmemind
@outofmemind 2 ай бұрын
The best thing about rust is converting strings to strings and u/isize to i32/64 and getting a panic error
@fswerneck
@fswerneck 2 ай бұрын
cool
@dipeshsamrawat7957
@dipeshsamrawat7957 2 ай бұрын
Thank you 😊
@this-is-bioman
@this-is-bioman 2 ай бұрын
Naming a package just "copy" is pure evil.
@Baldur1975
@Baldur1975 2 ай бұрын
Das ist verwirrend. warum nicht copy.replace?
@MrKerim2000
@MrKerim2000 2 ай бұрын
Einfach nur ein andere Art des imports?
@Baldur1975
@Baldur1975 2 ай бұрын
@@MrKerim2000 Ahh ok. replace wird ja von copy importiert und ist kein neues eigenes schlüsselwort. habe ich beim ersten schauen übersehen. Danke dir
@goid314
@goid314 2 ай бұрын
why can't we just do: point1 : Point = Point(x=1, y=1) point2 : Point = Point(x=5, y=1) ???
@juanjosefarina
@juanjosefarina 2 ай бұрын
If your class is as simply as Point, sure. If your class is big and complex you can make a deepcopy() and then change whatever attribute you need, but replace is more readable I guess
@sa-hq8jk
@sa-hq8jk 2 ай бұрын
kinda reminds me of Cell in Rust
@IunahYT
@IunahYT 2 ай бұрын
your IDE be like: I don't know what you mean
Why I Always Do This In Python
6:10
Indently
Рет қаралды 18 М.
5 Really Cool Python Functions
19:58
Indently
Рет қаралды 70 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 67 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 457 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Python Data Classes Are AMAZING! Here's Why
16:11
Tech With Tim
Рет қаралды 89 М.
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 71 М.
Transformers (how LLMs work) explained visually | DL5
27:14
3Blue1Brown
Рет қаралды 4,4 МЛН
Please Master This MAGIC Python Feature... 🪄
25:10
Tech With Tim
Рет қаралды 139 М.
Python's 5 Worst Features
19:44
Indently
Рет қаралды 114 М.