r/Python • • Sep 15 '20

Resource Python 3.9: All You need to know 👊

https://ayushi7rawat.hashnode.dev/python-39-all-you-need-to-know
1.2k Upvotes

204 comments sorted by

View all comments

240

u/kankyo Sep 15 '20

PEP 616, String methods to remove prefixes and suffixes

This is the big feature right here.

0

u/[deleted] Sep 15 '20

[deleted]

142

u/kankyo Sep 15 '20

Those people would have done s[:-4] previously anyway. Using the new stuff is WAY WAY better.

0

u/[deleted] Sep 15 '20

[deleted]

52

u/Ph0X Sep 15 '20 edited Sep 15 '20

I'm a man of culture, I do s.rsplit('.', 1)[0]

0

u/[deleted] Sep 15 '20

[deleted]

9

u/Ph0X Sep 15 '20

It's ambiguous which of the two behaviors is correct in that case, but if you want to remove all extensions, you can just switch to normal split. Of course that will break if it contains a period in the name, but that's also ambiguous. I guess you need a certain level of knowledge about what you're trying to achieve.

7

u/yad76 Sep 15 '20

I don't think it's ambiguous at all. It is a gzip file and so it has a `.gz` extension and your `rsplit` gets the correct result. The `.tar` is just reflecting the name of the gzipped file and not part of the extension of the current gzip file that we are currently concerned with.

3

u/Brian Sep 16 '20

I would agree for that case, but I would say one place where it does give the wrong answer (or at least, a different answer from splitext etc) is with dotfiles. Ie '.config' denotes a hidden file on unix, and pathlib and splitext will treat it as the stem '.config' with empty extension, rather than an empty sten with ".config" suffix.

→ More replies (1)
→ More replies (2)
→ More replies (3)

5

u/I_Say_Fool_Of_A_Took Sep 15 '20

This is the way. I'd never trust that the extension is going to be 3 chars. Aiff, wave, for instance.

15

u/super-porp-cola Sep 15 '20

I mean you don't even have to get obscure, there's .jpeg, .docx, and of course .py.

4

u/mipadi Sep 15 '20

Actually, the way is os.path.splitext(s)[0]. ;-)

→ More replies (3)

16

u/enjoytheshow Sep 15 '20

I feel personally attacked

→ More replies (24)

7

u/ianliu88 Sep 15 '20

Nice! Didn't know about pathlib.

3

u/eldrichride Sep 15 '20

VFX folk here are stuck in 2.7 and can only dream of Pathlib.

→ More replies (2)

4

u/c00lnerd314 Sep 15 '20

Out of curiosity, is there a downside to using this?

file_name.split('.')[-1]

13

u/call_me_arosa Sep 15 '20

Files don't necessarily have extensions

→ More replies (1)

24

u/[deleted] Sep 15 '20

[removed] — view removed comment

→ More replies (1)

12

u/DarFtr Sep 15 '20

I don't know for sure, but files can have multiple extension such as file that are in download state that are something.mp4.crowndowload It's just a guess anyway, I think it would usually work

→ More replies (1)

8

u/scruffie Sep 15 '20
path.with.dots/file
../file
.dotfile
...manydots
..

If you're not using pathlib, you should be using os.path.splitext, which handles all the above cases (and works with both bytes and strings).

→ More replies (3)
→ More replies (1)

0

u/[deleted] Sep 15 '20

[deleted]

9

u/kankyo Sep 15 '20

Yea this blog spam is incorrect. Not super surprising. It's blog spam.

2

u/Umlautica Sep 16 '20

I was wondering how the old str.rstrip("end") and new str.removesuffix("end") differ, and found the explanation in the pep

Because lstrip takes a string as its argument, it could be viewed as taking an iterable of length-1 strings. The API could, therefore, be generalized to accept any iterable of strings, which would be successively removed as prefixes. While this behavior would be consistent, it would not be obvious for users to have to call 'foobar'.lstrip(('foo',)) for the common use case of a single prefix.

Source: https://www.python.org/dev/peps/pep-0616/#expand-the-lstrip-and-rstrip-apis

1

u/ogahozy Sep 16 '20

Very good features

→ More replies (3)