Some New Features in Python 3.9
HTML-код
- Опубликовано: 31 июл 2024
- In this video I go over four new features in Python 3.9: new syntax for merging/updating dicts, native timezone support, the use of collection classes as opposed to the typing model for type support, and removeprefix and removesuffix method for strings.
Need one-on-one help with your project? I can help through my coaching program. Learn more here: prettyprinted.com/coaching
docs.python.org/release/3.9.0...
Get the code here: prettyprinted.com/l/wOS
00:00 - Intro
00:22 - Dict Merge/Update
03:33 - Native Timezones
06:22 - Collection Type Hints
08:48 - str.removeprefix
Twitter: / pretty_printed
Github: github.com/prettyprinted
EDIT: I am wrong. "b = b | a" is different from "b |= a" and "b |= a" is effectively identical to b.update(a) except that it returns b rather than None.
~Worth a mention that "b |= a" does *not* do the same thing as "b.update(a)". update modifies b, whereas |= creates a new dict and assigns b to it. If you had another variable c referencing the same dict as b, update would cause c to change but |= wouldn't.~
yeah ... when he was walking through the examples, I kept wondering what was happening w/r/t returning a new object vs modifying the existing object in place vs, as you point out - creating a new object and assigning it to one of the existing vars. I can see the usefulness, but the language syntax is making it tough to quickly assess if the result is what one intends - creating new, clobbering old etc.
Uhh, no. Have you actually tried it?
|= uses __ior__(), or "in-place or". It is NOT a syntactic sugar for b = b | a.
@@PanduPoluan Woah. Wild. I read the PEP to confirm, and I got misdirected by the motivation for the non in-place union.
@@Raserei408 Yeah, python likes to break the "(a $= b) ≡ (a = a $ b)" for some reason. it's the same with += in lists, it does a concat _and modifies the original_.
No! op= asignments work in place (as longs as it’s possible), because that is the actual meaning of these assignments!
this is the first time I hear someone pronounce "str" as "stir" instead of "string"
I say 'stir' when talking about the str class and string when talking about what str classes represent. Can be confusing
@@prettyprinted to be fair I say "stir" in my mind lol. but then again my professor pronounced char as "chir" lol
@@prettyprinted I do it the same time, actually. Also, I pronounce "JSON" as "J'sown" not "Jason", because JSON is clearly a Klingon name not a US/UK name...
Timezones natively without libraries is a biggie.
@O. R.C it's from dateutil.tz import tz
I'm so glad i found your youtube channel i watched your ultimate flask tutorial on packt and it was amazing!
Glad you liked it!
Please keep up such a great work for each new version.
Thanks for watching!
The dict update is a nice and consistent syntactic sugar matching what set() was able to do in 3.7 already, neat!
removeprefix gonna be really useful.
Thanks for the quick review 😊
You're welcome! Thanks for watching.
This is really good and easy to understand. Nice video
str.removepreffix sounds usefull to remove file extensions in a list comprehension to rename a bunch of files. And dict union share the same sintax as sets keeping the language logic.
I didn't think about file extensions. removesuffix does sound great for that. Thanks for watching!
@@prettyprinted Why adding redundant methods if you can already use .strip(), rstrip() and lstrip() ??
@@josemariasosaresendiz8286
>>> drs = ["Dr. Coomer", "Drake", "The Doctor"]
>>> names = [dr.lstrip("Dr. ") for dr in drs]
>>> names
['Coomer', 'ake', 'The Doctor']
@@josemariasosaresendiz8286 lstrip actually removes a set of characters, not a string prefix, and if you confuse what it does you can get some nasty bugs
@@beron_the_colossus This confused me too. This explains it: www.python.org/dev/peps/pep-0616/#id16
Yes it’s long overdue having to use a separate pytz module, though you would think they would just fix or extend datetime.timezone!
PEP8:
Method Names and Instance Variables
Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.
Python 3.9:
str.removeprefix
BOOOOOM!
You helped me a lot. Thank you
You're welcome!
Lot of times when I wished there was a standard library function str.removeprefix/sufix
Respect for using Star Trek doctors in doctor's list!😄
Could be the doctor from Doctor Who too
@@SurajTiwari020 I think it's The Doctor from Star Trek Voyager. They named this character the same way😅
All Star Trek doctors :)
Well I've figure it out... that's me... I'm The Doctor.... 😂 😂 😂
has
Thanks for the quick overview.
Removing typing import is moving python as typed language more and more.
Maybe we will be able to see performance improvement from that.
What do you think ?
The dictionary is not that great from my point of view as something already existed for this and using update is more pythonic.
I don't think the typing stuff will have any effect on performance since it's not evaluated. Thanks for watching!
Nice. I can finally stop re-implementing the removeprefix, and removesuffix functions in every python program!
Now if they would just fix the input and output streams, so you don't need to reopen them with errors="replace" every time, that would really make my day!
removePreFix() - on video time 11:20, you can also remove 2nd lines (names = []) lists
I have long used `a.split(b, 1)[-1]` to remove the prefix b from a in 1 line and `a.rsplit(b, 1)[0]` to remove the postfix b from a in 1 line. But remove prefix and remove postfix seems cleaner.
Thanks!
You're welcome!
I just can't see what's the point bringing these new operators for functions already well established...
Now we have two ways to perform the same functionality. Python used to be simple.
There should be one-- and preferably only one --obvious way to do it
Edit: Just to be clear the other changes are fine, I see myself making use of new String methods and maybe the new "timezone" lib.
These logical operators already existed for sets. If one thinks of dictionaries as sets of keys then it seems silly that python didn't have these operators for dictionaries in the first place.
It's also possible they may be planning to phase out, older, less intuitive, and less pythonic notation. I can't know for sure, but I think the best way to go about that is to release cleaner notation that everyone switches over to, and then later down the line announce the plan to deprecate old notation.
With the union operator (|), I'm hoping they end up allowing that for the Type classes. That way I can do str | int instead of Union[str, int]
This is planned for the next version (1.10): www.python.org/dev/peps/pep-0604/, and has already been successfully implemented and merged for cpython on github, see pull #21515: github.com/python/cpython/pull/21515
@@ewenlbh did you mean 3.10 or am i missing sth?
Which is your favourite new feature? I was taken by the fact that the http module now includes the “418 IM_A_TEAPOT” status code. ;)
docs.python.org/3/whatsnew/3.9.html
Great vid. I just don’t see myself merging dictionaries, but it looks easier than previous versions
I agree. Thanks for watching!
I feel the need to merge dictionaries came from the javascript community 👀
Id say the timezone stuff is the one thats good and long awaited. The rest i probably wont use that much.
dude can i get tensorflow for python 3.9 (link)
awesome
Thanks for watching!
Thx
Thanks for watching!
The str changes and dictionary merging will be nice. I do a lot of that and it's always fiddly code. Timezones are not so relevant to me but I'm sure others will find it useful. I saw there was greater flexibility with decorators too.
"Timezones are not so relevant to me but I'm sure others will find it useful"
Yes it can be useful from time to time.
are you asking us questions or did you learn that inflection at youtube presenting school?
Thanks good job! Do you plan to cover new decorators?
I didn't look into them much because I couldn't think of a use case. I'll take a second look eventually though.
I would like to point out that the dict unpacking method, like the pipe operator, does not modify any of the dictionaries, instead you get a new dict with the merged contents. The update method modifies one of the dictionaries, this might not be what you want.
Well, there's a reason why "augmented assignments" are also called "in-place operations"...
Well, time flies... I learnt Python from 2.5. Now I am using Python 2.7 and 3.7 but the latest version has become 3.9... Python is not like what I have learnt at first. I can even see the attempt to make Python strong-typed. (Probably we can make a declaration at the beginning of the script to make it strong-typed?)
However, I just want dev team to enhance the performance of Python so that I do not have to learn more and more new languages like Rust, Swift, Go, Julia, etc.
Great summary! I'm excited about typing, though i like that python is dynamically typed it's always a relief to have the option of types, its the same reason i prefer typescript over vanilla js. Will this make linting be available in VSCode?
I don't use linting in VS Code, but I'm sure there's some kind of type linter out there somewhere.
It's already in VSCode for some time. Install both Microsoft's "Python" extension (ms-python.python) and Microsoft's "Pylance" extension (ms-python.vscode-pylance)
What's the difference between str.removeprefix and str.lstrip?
names = [doctor.lstrip("Dr. ") for doctor in doctors]
will give you the same result.
Also why doesn't str.removeprefix follow PEP8? Shouldn't it be str.remove_prefix ?
lstrip removes a set of character not a fixed string. Look at the comments to Luis Munozs comment for an example.
I upgraded from 3.7 to 3.9 and have some tkinter widget placement issues. Has anyone else out there?
Is it better to download older versions such as 3.7 or 3.8? Because I think some of the modules and frameworks will not be valid for 3.9 version? Please answer...
The new versions only add features to the language, so they shouldn't break anything. But if you're working on an existing project, it might make sense to not upgrade unless you have to.
Typically the versioning is supposed to give you indications what you can update and what not.
The rule ("semantic versioning") is that a version is written like this x.y.z with the names major.minor.patch
A patch is supposed to introduce only fixes.
A minor update will bring new features but will work with older versions just fine.
Only a major update will bring potentially "breaking" changes (=changes that will stop code from working as intended).
Which means if any software packet or language follows these rules you can always upgrade from 3.x to 3.y if y>=x.
it would have been cool if we had a list or set as an argument for remove prefix or suffix function
that goes into the direction of regex
Yeah that would have been a good addition.
Or a sibling function removeprefixes([])
in all fairness, it already makes it so much easier to make your own implementation of that
@@aka5 not the point here. Having stuff like this embedded in the language itself is what would be cool. Of course it's easy to write it yourself but do you wanna copypasta that code snippet around your projects or import it from a custom library you've built? See my point?
@@novaria yes
I LOVE the Star Trek reference!
I was hoping someone would notice. Thanks for watching!
...
As I'm on Anaconda with its latency behind Python versions (i.e. I'm still working with Python 3.7), I'll probably build 'removepreffix' and 'removesuffix' into my collection of personal helper functions. Those seem too valuable not to have there.
Heyy , 👋👋👋
Please make video about
"Flask- pjax"
We are waiting bro .
I do not really like the disjunction thing. Sets use disjuction for union, as it’s basically the same thing. But for dictionaries, well, one would expect | to be commutative. So doing a | b when needed might be useful, but instead of a |= b it still makes more sense to use a.update(b), since the latter clearly states the direction and the intent.
The removeprefix stuff ... Well, it’s nice addition, but often you could just use lstrip for that.
All are great, but the last one(4) would be more useful.
I agree it would be useful in a bunch of situations. Thanks for watching!
So str.removeprefix does the same thing as re.sub('^Dr ','', str) ?
Yup, I hate it when they put in stupid functions already handled by better solutions
Except you dont need to include regex and easier for the eyes. Also you cant mess it up by accidently leaving the '^' from the start of the pattern. Probably less efficient also to use regex. There are already a lot of string based functions that can be solved with regex.
finishes it without importing re and makes the language more newbie friendly
The example given of removing titles from names is bad... how will this be used to remove other titles like Mr, Ms in addition to Dr? Just a big chain of if str.startswith()...
The difference is that "str.removeprefix" accepts the prefix as-is; if you use re.sub you must escape the special characters. Easy to do if the pattern is a literal, but hellish if the pattern is dynamic.
A contrived example: If the prefix to remove is "1.*", then you'll going to bork the whole string unless you take good care of escaping the "." and the "*". Again, doable if it's a literal, but if the process gets the prefix from another place (say, read from a file), then you need to put that in consideration.
no longer need stack over flow?
I have python 3.7 and now I want to install Python 3.9 (the new one). So, should I uninstalled my last python first then I install 3.9?
managing python versions is an absolute pain. My recommendation is to invest a little bit of time installing miniconda and learning a couple of its commands and how its environments work. It lets you cleanly keep and manage different python environments. That way, if you install libraries, they only install onto the environment you choose. You can also easily choose your python version through there. Unfortunately, miniconda might be a little slow to add the new 3.9 version to its available python versions, so they might not have it just yet.
If you're using Windows, you can simply install 3.9 in a different directory, and edit your PATH.
On Linux, you'll be limited to whatever package is provided by your distro's maintainer; best to use the "pyenv" tool.
First to comment! Also, you should make a tutorial on monthly subcriptions with paypal.
I definitely want to cover integrating Paypal again. Thanks for watching!
@@prettyprinted #InDjango
What comes after 3.9?
4.0?
3.10?
3.91?
3.10
so removeprefix is an extension function. can we create our own extension functions now in Python?
I'm not sure what you mean by an extension function, but removeprefix is just a method on the str class.
It is technically just another method of the string built-in. It's actually implemented by the interpreter, being faster than a python-land implementation. On another point, I don't believe extension methods will ever be accepted in Python, since it can easily confuse readers about "where this method comes from?"
@@prettyprinted just like what Kotlin has where you can extend a class with new functionality, the class being the receiver/type and the methog being the extension.
@@trustmub1 You cannot add new methods/properties to the `str` class. You can, however, inherit the `str` class and add your own sets of methods.
its kinda weird that 7:38 doesnt find an error, since mylist is still not of type list... is that normal or irrelevant here?
Types don't get evaluated at all. The exist for external libraries to verify the code.
@@prettyprinted yes, but the verification passes, despite mylist not being a list, but a string?
@@danielschmider5069 I believe type hinting in python is only for IDE and mypy to ensure a single type for a declared variable, doing
def do_print(a: int):
print(a)
do_print('b') # outputs: b
is fine and python can still run it because like Pretty Printed said, in runtime, python don't evaluate data types
@@danielschmider5069 "typing" in Python serves only as hints to the user/programmer. Assigning a string to the mylist variable that has been hinted as a list won't cause an error, because Python is not statically-typed.
Good IDEs will _warn_ you that "the value you assign to mylist _should_ be a list", but they won't stop you because Python itself doesn't care (the Python interpreter will 100% ignore the hints)
I realize Python doesnt interpret typehints, I'm specifically talking about mypy ignoring it. Is it because the same variable is defined a second time (line 6), ignoring the typehint that was introduced in line 3?
wow, I just noticed that I used lstrip / rstrip in a wrong way!!!
Wait for str.removeprefix would that be the same as doing str.replace("Dr. ", "")?
I guess this can be useful for only removing it if its a prefix/suffix, and keeping it if its in the middle of the str.
Yeah it will ignore anything in the middle.
seems like scala?
Actually except the second one others was interesting
my python is cmd. why
i want python upgrade its graphics desktop and mobile
and building mobile app on windows pc
pls
I already had read about the release .. I mostly thought “ugh, these bizarre new operators suck”... but seeing the first couple of minutes of this video, I am 100% sold now on | - that’s a keeper (that should happen in other languages even)
mine looks like cmd
omg 14:27 why did that take so long
Apologies, but I am EXTREMELY doubtful that { **a, **b } is equivalent to a.update(b) in Python 3.9
For one, this would break a lot of legacy code that relies on the existing semantics for { **a, **b } which is to construct a new dictionary and to leave a, b unchanged.
The only sense in which your claim seems to be correct is that in the NEW dictionary, if both 'a' and 'b' contain a key X, the new dictionary will take the value in b in preference to the value in a.
I tried scanning through "What's New In Python 3.9" and could not corroborate your claim there.
I would be grateful if you could point me to the specific section in case I've missed it.
- Dion
Still no i++ in python
i+= 1 is only one extra character :)
it's actually 3 more because spacing
I like the performance improvements of 3.7 + 3.8, and the friendlier sugars in 3.9 ... but too bad I still have to target 3.6 because at this moment it's the most widely deployed version (Ubuntu 18.04 comes with 3.6)
You shouldn't just limit your libraries or software to specific versions because they're the most deployed, use the versions that have features that you want to use. If your software that you're writing is actually good for the job and is worth it, they will bother updating Python for it.
@@ribosomerocker Well, installing non-mission-critical 3rd party libraries in Enterprise Production is a big no-no. Most features in 3.7
He build a time machine!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This looks completely different from mine, why is that. Mine is just the basic, standard black screen.
Me too. Is there anyway to make it look like his?
@@JustLaura404 I think I figured it out, you have to download pycharm
@@snorribjarnason8408 kk Hope it works. Thanks for the feedback
@@snorribjarnason8408 I finally figured it out. You don't have to download pycharm. Just go to search on your windows , look for Python IDLE not the one u opened but a different one. Open it and then that's it
There is a remove prefix but not a add prefix? 🤔
Just append to the beginning of the string
@@ribosomerocker What if the prefix already exists in the beginning of the string?
Then don't? It's like saying removeprefix is faulty because it doesn't account for when the prefix was already removed and then it gets removed again if it exists
@@ribosomerocker The whole point of removeprefix is to only remove if the prefix exists and the implementation could be a simple if as shown in the video, the same thing is valid for addprefix only changing the fact that it would add instead of remove...
@@SrIgort Except something that already does that already exists... They can't just add something that is almost the same as something else (which is just adding up strings.) what you're looking for can be done in one line of code, ``prefix + string if not string.startswith(prefix)``
Beverly Crusher was hot
Thanks for watching!
@@prettyprinted My pleasure - I really like your channel!
so if you like just go ahead and answer it and then i'll go ahead if I just want to and so on
Doctor who reference yo
Not Doctor Who, just Star Trek doctors here!:)
Star Trek
Isn't removeprefix same as lstrip?
So you can add types to Python... Then what's the bloody point in using it?
Most likely for readability.
very similar to [doctor.replace("Dr .", "") for doctor in doctor]
similar but not identical, the behavior is different when there is a "Dr. " in other places in the string, other than the start.
More stuff to force everything to be updated...
Nothing was removed in this update though I don't think? It will probably be the case for python 4 but this update just added some functionality/alternative uses. Unless I have missed something
Using '|' is for Dict-Merging is beyond stupid. There was nothing wrong with {**dict_a, **dict_b}, especially since it was kinda unique. Including AugAssign as well, takes the Zen of Python and throws it out of the window.
same feeling, I used to like python syntax sugar since it's pretty easy comparing to others. But this one is purely stupid for the sake of "simplification" because the "bitwise operator" or " | " somehow is used in so many cases across different languages besides python. This one brings a lot of confusions for me, and dilute, pollute the original purpose of the operator.
Tsk tsk ... what would Guido say?
And the weirdest new feature: http status code 418 IM_A_TEAPOT
Value add to confusion ratio for new dictionary merge syntax is low... if something is broken, fix it or replace it, don’t add yet another way of doing it that’s slightly different than the original.
This allows dicts to be handled similarly to sets without having to put in an "if isinstance()" or casting.
doctor = [x.replace("Dr. ","") for x in doctorlist]
It might replace Dr. or any other target from the rest of the string. string.removeprefix() only removes from the start.
@@surajvkothari This is so true...
Using pipe with two variables - bitwise OR, why the hell they made this confusion? Is it more clear than simple update? Damn, I'd better switch to Fortran
For someone who just started learning python it just confused things even.more..
Don't worry about these features yet. The things you see in every Python tutorial are the things that matter the most.
What got you confused, bro? Anyhow, you may not have to worry about these features just yet. Many libraries support only Python 3.6 or 3.7 (well, it depends on what field you work with).
Can someone tell why someone should learn a language that is still under development of features that most people would use? Legit question.. Don't want no sissy answers please...thnx
First, im curious to know what your reasoning is for someone NOT to learn a language that is still under development. Id like to hear your thoughts
now to my perspective: most if not all, languages are actively under development until theyre old and deprecated. python 2.x is no longer under development. using your logic, youd want to use that one. but then the question is why would you want to use one thats old/deprecated/no longer being updated? also it comes down to what most people use. FORTRAN no longer gets any real updates (as far as I know) but no one uses it anymore (for the most part), so why would you use FORTRAN? these are all valid questions
(also you could have stopped at "legit question", saying the rest made you come off a bit rude. not saying anything bad about you or your question but i wanted to mention it just incase you didnt catch it)
📢📢📢WANNA HAVE PYTHON 4.0
Give it a few years.
This is why I hate python! You never know if for something you're stuck doing there is a simple syntax like "a | ^ b .... -b | (a) >>> a" !!!
This is no reason to hate Python for... Ignorance is really not a reason to hate anything. It's like hating C++ for not knowing if there's a specific function already written for you to do the job but you don't know it.
@@ribosomerocker Except with C++ or other languages there is an finite number of functions and syntaxes. I can bet you a C++ programmers with 5 years of experience knows almost 80% of the available syntaxes and functions while a python programmer with the same amount of experience would only know about 15% of them.
Apart from that, in C++, if you wonder which class or namespace of a certain library contains TimeSeries functions, you write "TimeSeries", while in python you gotta know whatever self-made "pandas" "kerbomint" "tityfish" "retronaut" shitty name they have put over their libraries...
@@ldohlj1 Python can not bave infinite numbers of functions and syntaxes... A C++ developer with 5 years of experience, while still only knowing 80% currently, will have less knowledge of C++ as years go by if they don't actually see what features are being added in every version of C++. Same goes with python, 5 years of experience would be enough to know basically the entirety of the standard library and syntax features, hell... I have only a year, and I am pretty sure I know more than 80% of Python, as of right now. I never heard of titty fishes in Python's standard library though.
so they just hlaf-merged shitty libraries to the core and implemented a horrible idiom to update dictionaries, don't mind why Guido left this train-wreck of a language.
In fact, it seems like this new behaviour was even supported by Guido himself. He supposedly stepped down from BDFL position because of core developers backslash on this release , starting from Py 3.8
Kinda sad news to me, since I do share the same concerns as the people against these new syntactic sugar.
Feel free to stick to VBA.
I like this idiom, actually.
Now I can have same processing for set & dict.
def do_something(o1, o2):
rslt = copy.deepcopy(o1)
rslt |= o2
for i in rslt:
# do things
I can call do_something with a pair of sets or a pair of dicts and the code stays the same. Without the "dict union" operator, I'll have to do an ugly "if isinstance()" or casting.
@@anirangoncalvesbr Well, Guido's not getting any younger, and he's getting tired of all the too-emotional debates as Python more and more popular. Newer members of the community might be "a bit too vocal" and that kind of "less respective" behavior tends to be contagious. PEP 572 was "the last straw" in the sense that Guido took a step back, saw that he can no longer be too invested in emotional things, and decided to no longer be the dictator.
Also, Guido did not leave Python. He just stepped down from being the BDFL. He's still part of the core Python developers, and still actively give input & advice, but now people cannot dump things into his lap and expect him to make a decision on polarizing issues.
Every update, I like python less.
Bored 😲
So, nothing new, which will require a whole new version.
Python already has stupid syntax, and there're introducing more stupid operators.
they created a whole new interpreter as well, amongst a bunch of other stuff not included in this 10 minute video about 4 new things.
Yeah these are the features that jumped out at me. There are more things included in the release.
(1) You are not required to use the new "dict union" operator. It's optional. You can keep using .update()
(2) Implementing the "union" operator is actually a good decision, since it allows dicts to be processed similarly to sets. No more casting and/or "if isinstance()" to implement different operations for dicts and sets.
@@PanduPoluan Correct. You are not required to use python also.
So do you start every sentence with "so"? Go back and listen to yourself. I had to stop watching after 20 "so's" before 2 minutes in, or so. So I hope you don't always talk like this, for your colleagues sake.
Too slow. I’ll go look it up.
Python seems to be losing its way with these unnecessary additions. It was simple and beautiful, now its getting short cuts which make it ugly.
Why is it ugly? If any, this allows dicts to be processed similarly to sets. No more casting and/or "if isinstance()" ladders to implement a similar (but not same) operation.
Why am I here I don’t even know how to code
You can always start learning.
@@prettyprinted yes i keep trying its been five years