r/Python • u/cheerfulboy • Sep 15 '20
Resource Python 3.9: All You need to know 👊
https://ayushi7rawat.hashnode.dev/python-39-all-you-need-to-know125
u/Hopeful-Guess5280 Sep 15 '20
The new syntax for dictionary unions is looking cool.
50
u/Sigg3net Sep 15 '20
For a second I read that as
dictionary unicornsand I shuddered. It's some mythological, elusive key that might not exist.4
u/olaf_svengaard Sep 15 '20
Have you not seen “The Last Unicorn”? Where you see a bug I see a feature :)
17
u/anyonethinkingabout Sep 15 '20
It looks cool, but it's yet another unneeded feature that isn't clear upon reading the code. There already is a method, and you could do it in a short snippet as well. So why add it?
13
52
u/energybased Sep 15 '20
It replaces
{**a, **b}witha | b. That's clearly much better.84
u/its_a_gibibyte Sep 15 '20
The first one is clearly better. It shows that you're building a new dictionary
{ }and you want to include all the elements of a and the elements of b.The second one looks like a boolean expression for or.
57
u/vaevicitis Sep 15 '20
It also looks like a set Union, which is essentially what the operation is for dicts
2
u/I_LIKE_FACE_TATTOOS Sep 15 '20
Wait what? Isn't set union "∪"? Is there an alternative symbol I'm unaware of? 😶
12
2
u/copperfield42 python enthusiast Sep 15 '20
Yes, in symbolic notation, but you can't easily type this "∪" with your keyboard, so | is used instead because is available in every keyboard and doesn't need to know some esoteric key combination for it.
Same with the rest of set operation like intersection, and etc.
2
u/ianliu88 Sep 15 '20
Although it is not commutative.
19
u/bakery2k Sep 15 '20
Set union isn't always commutative either:
>>> class A: ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __eq__(self, other): ... return self.x == other.x if isinstance(other, A) else NotImplemented ... def __hash__(self): ... return hash(self.x) ... def __repr__(self): ... return f'A({self.x}, {self.y})' ... >>> {A(0, 0)} | {A(0, 1)} {A(0, 0)} >>> {A(0, 1)} | {A(0, 0)} {A(0, 1)}12
u/scruffie Sep 15 '20
However, that's commutative with respect to the equality you defined, which is all we can expect.
1
u/ianliu88 Sep 16 '20
Well, I guess if you define dictionary equality by their keys, then the operation would be commutative, but that's generally not the case.
5
u/stevenjd Sep 16 '20
Although it is not commutative.
Neither is
{**a, **b}.Do you do arithmetic on floats? Even addition on floats isn't commutative:
a + b + cis not always the same asc + b + a.Commutativity is over-rated.
1
u/ianliu88 Sep 16 '20
I only pointed out the fact that
{**a, **b}isn't a union operation, as stated by the previous comment. It is a dict update, and it is expected for it not to be commutative.→ More replies (1)9
u/KFUP Sep 15 '20
The second one looks like a boolean expression for or.
It kinda acts like an 'or', since it is getting elements that are in either a 'or' b, it would be cool if it has an 'and' operator that only gets what is shared between the two.
6
u/energybased Sep 15 '20 edited Sep 15 '20
That operator exists for sets, but for dictionaries, what is
{1: 'a'} & {1: 'b'}? I guess it should prefer the second value to stay consistent? (== {1: 'b'})I think it's better to be explicit here and use a dict comprehension.
5
u/XtremeGoose f'I only use Py {sys.version[:3]}' Sep 15 '20
Check the pep, they talk about dict intersections.
{**x, **y}is ugly IMO, if you don't like the union operator, used = d1.copy() d.update(d2)A dict comprehension is really hard to read
{k: v for k, v in itertools.chain(d1.items(), d2.items())}→ More replies (1)→ More replies (1)24
u/energybased Sep 15 '20
In that case, you don't like the set union operator either, which has been in Python for at least a decade. This operator replaces
set(*a, *b)witha | b.1
2
u/SeanBrax Sep 15 '20
In what sense? Conciseness, sure. Readability? Definitely not.
4
u/energybased Sep 15 '20
I agree that it's a personal question. I find
|more readable, especially in multiline expressions.1
u/SeanBrax Sep 15 '20
How does | read more clearly as a union than the proposed alternative?
2
u/energybased Sep 15 '20
I agree that it's a personal question. I find | more readable, especially in multiline expressions.
1
u/SeanBrax Sep 15 '20
I didn’t ask you to repeat yourself. I just asked why in your opinion that’s more readable.
2
u/energybased Sep 15 '20
It's a preference. I guess if I'm forced to think about it, I see dictionary union as a binary operation, so it makes sense to me for it to be encoded using a binary operator.
Also, at the outset of the generalized unpacking syntax ({**, **}, etc.) people on Python ideas did not like its use as a dictionary union.
1
u/flying-sheep Sep 16 '20
I think it's equally clear, and more consistent with the already existing set operations.
1
8
u/Weerdo5255 Sep 15 '20
Ugh, I'm such a nerd but I'm excited as hell for the dictionary unions. Make life so much easier!
1
u/Pythonistar Sep 15 '20
Initially, I was excited, but then I wondered:
What if the keys are the same? which K/V gets kept?
Apparently, K/V pairs with the same key from the 2nd dict overwrite values from the first. Makes sense if you think about it...
But that's kinda side-effect-y and not necessarily obvious.
I agree with /u/XtremeGoose in that
d = d1.copy(); d.update(d2)is clearer.You're very clearly copying dictionary 1 to a new dict and then merging dictionary 2 into 1 overwriting any duplicate keys.
I favor 2 lines of clear code over 1 line of syntactic sugar which is much less obvious.
8
u/XtremeGoose f'I only use Py {sys.version[:3]}' Sep 15 '20
I'm not saying it's cleaner ;) just better thank the
{**d1, **d2}.I'm definitely happy with right associative unions. As said elsewhere in the comments, set unions also act this way.
4
u/Pythonistar Sep 15 '20
Yeah, I think I agree. The
**unpack operator has never pleased me with how it looks. I mean, I know what it does, but it doesn't look clean...happy with right associative unions. As said elsewhere in the comments, set unions also act this way.
Yeah, it makes sense. The left side gets copied first, the right side overwrites if there are any "same" sets/keys. (for whatever definition of equality that you're using...) You just have to know that's the behavior.
0
u/stevenjd Sep 16 '20
Apparently, K/V pairs with the same key from the 2nd dict overwrite values from the first. Makes sense if you think about it... But that's kinda side-effect-y and not necessarily obvious.
As opposed to your suggestion:
d = d1.copy() d.update(d2)which is totally side-effect-y and not necessarily obvious, plus it isn't an expression so it requires an unnecessary temporary variable d.
1
u/Pythonistar Sep 16 '20 edited Sep 16 '20
which is totally side-effect-y and not necessarily obvious
Well, they're all side-effect-y. Any merging of a dictionary is side-effect-y. (in contrast, Merging of Sets is not since ostensibly you can test for equality and remove duplicates.)
plus it isn't an expression so it requires an unnecessary temporary variable d.
What's that got to do with the price of tea in china?
Expression or no, there's always a new variable whether you see it or not.
d = d1.copy() d.update(d2)No, that's abundantly clear.
Copy dict to new variable
Update all the key values in that copy with the key values from the second one (because it comes second!)
It's crystal clear.
The former method is unclear just by looking at it. Instead you have to know how it works.
1
u/stevenjd Sep 18 '20
Expression or no, there's always a new variable whether you see it or not.
What? No. Just no.
Here is a thing with a temporary variable:
x = 1+1 print(x)And here is one without:
print(1+1)There is no "x" variable, or any other variable, created in the second example. You can inspect
locals()or the byte-code if you don't believe me.Instead you have to know how it works.
As opposed to copy and update, which as we all know, little babies too young to walk or talk are born knowing how they work. /s
-2
Sep 15 '20
The feature is great, but I don't get the decision to add a new operator for such a simple operation, they could've just used a +.
15
u/bakery2k Sep 15 '20
It's not a new operator.
|is already used for bitwise-or of integers, e.g.5 | 3 == 7.0
10
0
Sep 15 '20
[deleted]
6
4
u/troyunrau ... Sep 15 '20 edited Sep 15 '20
Has existed for ages. I implemented a geometry routine in python 2.7 which used it in the context of computational solid geometry. If you had to solid objects defined by a a collection of polygons, you could take the union or intersection of those objects using the overloaded python set syntax. Made for really clean code. Pity I never did anything with it.
a = sphere(r=1) b = cube(1,1,1) c = a & b # intersection of d = a + b # union of e = a - b # difference ofEtc. The goal was to create a library that would act sort of like openscad in python. Honestly, I should return to it some day, but I saw my tail and got distracted
1
4
62
u/xX__NaN__Xx Sep 15 '20
Yooo.. when did this happen!!? I just recently upgraded to 3.8, and 3.9 has already been launched
15
Sep 15 '20
I’m still in 3.7....
18
u/wxtrails Sep 15 '20
😅 2.7
52
14
Sep 15 '20
Corporate legacy code or academic legacy code?
8
u/WillardWhite import this Sep 15 '20
Stuck with VFX programs that are still holding on to 2.7 T.T (cries in Autodesk)
5
u/wxtrails Sep 15 '20
Corporate. Simultaneously hearing different teams shout "That's out of date and insecure, you must upgrade!" and "Ain't nobody got time for that!". So it sits.
1
1
u/i4mn30 Sep 15 '20
Hobby project for me
20
3
u/Lonke Sep 15 '20
But why
2
u/i4mn30 Sep 16 '20
I just don't work on it actively any more.
It's a simple app, and porting to 3 would not benefit me at all. Just syntax change and a few goodies wouldn't make a huge improvement as it's not for some client and needs constant maintenance which would obviously require me to port it to 3
0
0
12
u/SomeShittyDeveloper Sep 15 '20
So will there still be a need for pytz after Python 3.9?
4
u/raziel2p Sep 15 '20
pytz might get more frequent updates. haven't looked into if zoneinfo supports updating in other ways than upgrading python itself.
11
u/DDFoster96 Sep 15 '20
Python will get its timezone info from the operating system if it provides it, or from the core dev-supported tzdata package on PyPI if not (e.g. Windows).
4
u/PeridexisErrant Sep 16 '20
No, and I'd recommend against
pytzdue to incompatibilities with PEP495 (which, in fairness, it predates).See https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html for details - Paul Ganssle is the maintainer of
dateutil, a CPython core dev, and author of the newzoneinfolibrary!
20
u/HughMacdonald Sep 15 '20
In your type hinting example, you're missing a d from the start of the function definition
9
7
u/master3243 Sep 15 '20
I actually kept staring at that "ef" wondering what the hell kind of type hinting that was. Thanks for clarifying
7
3
u/BAG0N Sep 15 '20
That's pretty dope. BTW can someone explain that performance boost with vectorcall easier?..
2
2
u/jzia93 Sep 15 '20
I'm still on 3.6 and I can't quite move off it, had a lot of 3.8 problems with Redis in particular and trying to run Unix commands on Windows 10.
Can anyone recommend why should we upgrade earlier than needed?
2
u/proverbialbunny Data Scientist Sep 16 '20
3.8 was a large change causing some services difficulty due to asyncio changes. Some of my code is on 3.7 currently for this reason. (And some on 3.8.) I would need to update my multiprocess code as for some sort of undocumented reason 3.8 breaks it.
However, it's been long enough that large name packages and services should work just fine with 3.8 now. I would hope at this point Redis would have no problem.
I would give it another try at least once every 12 if not 6 months. It's probably fine now.
1
u/jzia93 Sep 16 '20
Thanks, good summary.
Actually this was last week. It's to do with running Ubuntu on Windows. Not ideal I know, but it's an officially supported extension by Microsoft so it's a shame they haven't fixed this.
0
12
u/productive_guy123 Sep 15 '20
Crazy how some companies are on python 2.0 while we're so close to 4.0
53
48
Sep 15 '20 edited Jun 20 '23
Unfortunately Reddit has choosen the path of corporate greed. This is no longer a user based forum but a emotionless money machine. Good buy redditors. -- mass edited with https://redact.dev/
12
18
u/kankyo Sep 15 '20
Afaik the plan is to go to 4.0 just to trigger the removal of the deprecated stuff that is scheduled for 4.0. So it can really happen at any time when the core devs feel like it. There will be no big disruption.
0
Sep 15 '20
[deleted]
9
u/kankyo Sep 15 '20
There are depreciation warnings in python NOW about 4.0. It's happening. It's just going to be a non-event, the way it should be.
→ More replies (2)1
u/Mezzomaniac Sep 16 '20
I haven’t come across those deprecation warnings. What do they relate to?
2
u/kankyo Sep 16 '20
Hmm... Seems like they've changed them so now they are about python 3.10. I am not a fan of that. https://www.python.org/dev/peps/pep-0563/
4
u/toyg Sep 15 '20
there is no substantial language change that would justify rewrites to an incompatible python version
4.0 will likely NOT be an incompatible version at all. Nobody wants to repeat 3.0. The most they’ll do is removing long-deprecated features. Code that runs “4.0 - 0.1” will work fine on 4.0, whenever that comes.
31
Sep 15 '20
None of my customers are on 2.x anymore. I make it a requirement that they upgrade to 3.x and I do not write them anything in 2.x compatibility syntax. I'm doing my part to help fix the world one server at a time.
11
Sep 15 '20
You’re doing God’s work thank you. I’m in academia and it has been a nightmare. I force everyone I work with to be in python 3. I will not budge, nor accept absolutely anything in python 2.
1
u/Bahet Sep 15 '20
Out of curiosity, what do you do? I’ve never done things server-related with Python
8
Sep 15 '20
I work for a company that develops software... Sorry that I can't share what software.
I only write Python when a customer needs a custom scripted solution to integrate our software with other items in their environment. Sometimes it's via API, sometimes it's parsing other data files and ingesting them into our software, sometimes it's just automating activity within our software to custom tailor it to their environment. I do this same work on bash/powershell based on their requirements, but when they say Python, I tell them that it's a waste of their money to pay for custom work to be written in a deprecated and unsupported version of a language that is being actively discontinued. Nobody ever argues because they recognize that having longer living code is a better use of their money.
5
Sep 15 '20
But that isn't particularly controversial, since there isn't a bunch of legacy code that your work depends on. Most people starting new projects will do so in Python 3. It's the millions of lines of code in legacy projects that still need to be maintained that's the showstopper.
1
Sep 15 '20
No, my work often depends on legacy code.
2
Sep 15 '20
Yes, but does it operate in the same environment? If you are adding code to a legacy Python 2 project or importing it as a library, you must also be on Python 2. If you are accessing legacy code through REST, a message queue, or a job queue, then the two can operate independently.
If your application is in fact adding code to a legacy project, I would argue you are losing revenue by forcing clients to Python 3. I have worked at large F500 companies with so many LOC in Python 2 that such a requirement would be an instant deal breaker - you would lose the sale.
3
Sep 15 '20
Not quite. We are deprecating and replacing old sections at times. Code bases I work with aren't millions of lines individually, usually around 500-5k for a single thing. Updating from dead python to current isn't as big of an ask and it's something they are eventually going to have to do so it gets added in as part of the project scope when doing new work or additions.
5
u/sdf_iain Sep 15 '20 edited Sep 15 '20
Python
usesdoes not use semantic versioning, but rather it’s own versioning scheme (assuming I’m reading that PEP right, it’s written to be broadly applicable).However, the same point stands numbering in any particular spot may exceed ten (I.e. 0.11.13) without automatically incrementing any other section.
EDIT: assumptions changed to be, hopefully, more accurate
9
u/bakery2k Sep 15 '20 edited Sep 15 '20
Python uses semantic versioning
Not exactly. Minor releases often contain some breaking changes.
2
Sep 15 '20
Basically every project in the history of the world that “adheres to semver”
1
u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Sep 16 '20
That's because semantic versioning is way too idealistic. It equates all breaking changes to each other. But some breaking changes are much more significant than others.
(57 -> 58): dropped support for x86might be a much more significant change than(56 -> 57): dropped support for Z80but they look almost the same as each other from the version numbering.2
1
Sep 15 '20
Comment to your edit:
"This PEP describes a scheme for identifying versions of Python software distributions, and declaring dependencies on particular versions."
That has nothing to do with Python itself.
1
u/sdf_iain Sep 15 '20
Is Python not a Python software distribution?
It seems to be written broadly enough to apply to the entire ecosystem, depending on how you read it. And Python versions seem (I didn’t look in detail) to fit with the constraints specified. Of course I could be totally wrong, but then what PEP covers the versioning of Python itself.
1
Sep 15 '20
Did you read the definitions? Nowhere is Python itself mentioned.
3
u/sdf_iain Sep 15 '20
We could go on about this for a while, but given that this PEP defines how to do versioning for a "Project" (software components that are made available for integration) and PEP20 says "There should be one-- and preferably only one --obvious way to do it", unless you can find a PEP that defines how to version Python itself, PEP-440 probably applies.
→ More replies (1)1
u/proverbialbunny Data Scientist Sep 16 '20
Really? Isn't Python 2 officially EOL at this point? Many services have stopped supporting it recently. If companies do not upgrade they'll find themselves in the Isles of Legacy. (Which I just made up.)
0
Sep 15 '20
[deleted]
2
u/PeridexisErrant Sep 16 '20
And you can use
flake8-2020to check for uses ofsys.versionorsys.version_infothat will break with a two-digit minor version!
1
u/ZarsBars Sep 15 '20
Is python 4 close to release or something? Seems like the updates are getting up there
9
2
1
1
u/accept-the-mystery Sep 16 '20
how is removesuffix different from the existing rstrip/lstrip? perhaps the new function only applies to the first occurrence?
in any case, great to see the new version 🙂🙂
0
u/lzantal Sep 15 '20
Not too crazy about dictionary unions syntax. It seems cryptic. I am glad they are making it in but I wish there was cleaner way.
4
u/mipadi Sep 15 '20
I love the idea, but I think it should be a method, e.g,
c = a.merge(b), instead.0
Sep 15 '20
[deleted]
2
u/mipadi Sep 15 '20
|isn't;update()corresponds to|=.Anyway, that's my point: I think it should be a method, rather than an operator.
1
u/ThePoulpator Sep 15 '20 edited Sep 15 '20
I have not tested it myself, but since operator are overloaded, methods should work too:
a | nis equivalent toa.__xor__(b)
a |= nis equivalent toa.__ixor__(b)edit:
__or__actually3
u/mipadi Sep 15 '20
|is implemented using__or__, and|=is implemented using__ior__. (__xor__is^).And I mean...yeah, that's how Python works, you overload special methods to overload operators. I just mean that I'd prefer a method called, e.g.,
merge(), to form parity withupdate(), instead of using operators. Explicit is better than implicit, right everyone? That's what Pythonists always exclaim, right? :-)(In general I have no problem with overloaded operators, but I don't think the use here is very elegant.)
1
1
u/its_Aqdas Sep 16 '20
List=[1,1,2,3,5,8,13] print(list[list[4]])
Please explain how the answer is 8
4
u/illpicknamelater Sep 16 '20
list[list[4]]->list[5]->8Explanation:
list[4]returns5which is the 4th (the first index is 0, remember?) element of thelistsequence. Hence,list[5]returns83
-13
u/KripC2160 Sep 15 '20
Can’t wait for 4.0 (unless it’s going to be 3.9.1 lol)
19
u/Peanutbutter_Warrior Sep 15 '20
3.9.1 will probably be bug fixes and the like, nothing major. It'll be 3.10 for the next major release. It'll only become python 4 when big changes are made that make it not backwards compatible
6
u/metaliving Sep 15 '20
There's going to be a 3.10. There's also no information on python 4.0 ever coming out. After 3.10 there can always be 3.11, 3.12, ... 3.infinity.
Also, just curious, why can't you wait for 4.0?
4
u/saihemanth121 Sep 15 '20
It'll become 4.x only if there are breaking changes and it's no longer backward compatible. It will go to 3.10, 3.11,....3.21 and so on. It's not decimal notation, the dot seperates integers in decreasing order of impact
0
Sep 16 '20
[deleted]
1
u/gmes78 Sep 16 '20
No, some stuff has been deprecated and removed.
1
u/Caligatio Pythonista Sep 16 '20
Several things have been deprecated but I'm unaware of any removals. Have things actually been removed? I've seen conditional module dependencies check for minimum version, never maximum version.
1
u/gmes78 Sep 16 '20
Yes, they have. You can check the release notes, it's all detailed there. For this release, there were quite a bunch of removals.
Another way is to run your code passing
-X devto the interpreter. You'll be able to see the deprecation warnings, as well as which version those deprecated items will be removed.1
0
Sep 15 '20
[deleted]
39
u/XtremeGoose f'I only use Py {sys.version[:3]}' Sep 15 '20
Or even better, the what's new in python 3.9 page
13
u/Ph0X Sep 15 '20
Yeah that's much closer. Actual PEPs are very hard to parse for normal people and I definitely would not recommend those as a way to find out what's new for everyone.
I would consider this blog post one level simpler than What's New, with clean examples. There's nothing wrong with having various level of complexity for various needs.
63
u/energybased Sep 15 '20
Yes, but this is still a well-written, well-presented, succinct article. I think some people might prefer it.
28
2
1
u/TheIncorrigible1 `__import__('rich').get_console().log(':100:')` Sep 15 '20
Good link. I'm disappointed that they don't separate the words for some reason in
removeprefix/suffix. Why do they constantly make inconsistent decisions like this?4
u/miggaz_elquez Sep 15 '20
Because it's consistent with all str methods :
str.isupper,str.startswith, ...0
Sep 15 '20
[deleted]
3
u/TheIncorrigible1 `__import__('rich').get_console().log(':100:')` Sep 15 '20
remove_prefix, yes.removePrefixwould really break me just like using theloggingpackage does already.0
Sep 15 '20
[deleted]
0
Sep 15 '20
[deleted]
0
Sep 15 '20
[deleted]
2
u/execrator Sep 15 '20
Not for your own code of course, but we're talking about the standard library here. If every contributor used their own style things would be a mess (not that there aren't a couple of messes already!)
→ More replies (1)
240
u/kankyo Sep 15 '20
This is the big feature right here.