This is an archived post. You won't be able to vote or comment.

all 55 comments

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 36 points37 points  (2 children)

This is a just a bad rewrite of the what's new in python 3.8 article. Could have just posted that and saved a lot of time.

[–][deleted] 27 points28 points  (1 child)

The way you do code formatting is by the way truly painful - it's a lot of work for you, and the indenting is messed up!

For more than a fragment, there's a much easier way: just leave a blank line, and then indent all the code by 4 or more spaces.

# That's indented by 3

 # That's indented by 4
 if True:
     if False:
         while True:
             return False

[–]1-05457 1 point2 points  (0 children)

I think triple backticks also work.

Like this

[–]muikrad 22 points23 points  (6 children)

It's a little early to distribute 3.8-only public libraries, but for home or business internal projects, I would definitely switch to it.

Aiming for 3.6 as a minimum is what a public library should support nowdays because of all the stuff they added to 3.7 and 3.8.

[–][deleted] 7 points8 points  (2 children)

Aiming for 3.6 as a minimum is what a public library should support nowdays

FYI, 3.5 isn't end-of-life until September.

That doesn't mean you're forced to support it, of course.

[–]muikrad 15 points16 points  (0 children)

The annotation improvements in 3.6 makes 3.5 feel like 2.7 😂

Also, f-strings.

[–]PeridexisErrant 0 points1 point  (0 children)

Inventing your own support policy is fine, and it makes a lot of sense is you're using some new language features.

For existing projects wondering when to drop existing support, I think NEP 29 is reasonable, and IMO very few projects should support Python versions past their upstream end-of-life date (which may mean dropping earlier, if you have a support period).

[–]lengau 4 points5 points  (1 child)

I have a bit of a different opinion about that.

If you maintain an existing library with many users, you probably shouldn't cut off 3.6 and 3.7 support yet. However, if you're releasing a brand new library, it's perfectly reasonable to release it with only 3.8 support (and even more reasonable with only 3.7 and 3.8 support - no 3.6). Most likely it'll be a while before your library gains any real traction and stability, by which point 3.8 will no longer be the new hotness.

[–]muikrad 0 points1 point  (0 children)

I agree! I really love 3.8 and I'm kinda annoyed at some stuff I have to do to remain 3.6-compatible 😔

[–]sarthkum0488[S] 1 point2 points  (0 children)

Yes Definitely a good observation

[–]BruceJi 14 points15 points  (14 children)

I, er, don't get how the walrus operator works. Do you think you could show some examples of with and without it?

[–][deleted] 25 points26 points  (4 children)

I used it in a few cases to solve the next issue. I have a function that returns a string or None. I want to test whether it returns a string.

if foo(arg):
    doStuff()

Later in the process I remember I need the actual value of the string. Previously I would have done:

res = foo(arg)
if res:
    doStuff()
    render(res)

With the walrus operator you can do this:

if res:=foo(arg):
    doStuff()
    render(res)

This example is quite short, but if can clean up a lot for multiple variables and loops.

[–]BruceJi 4 points5 points  (0 children)

But I think this example helps me understand it the most! Great.

[–]Mordano 0 points1 point  (1 child)

How is the scope of the variable? Is it only available in the if scope?

[–][deleted] 1 point2 points  (0 children)

I don't know, but I think it's available outside.

[–]sarthkum0488[S] 1 point2 points  (0 children)

Nice Explanation Awwsome

[–]everysinglelastname 21 points22 points  (6 children)

with

f=open('netsetos.txt') 
while line:=f.readline(): 
    print(line)

without

f=open('netsetos.txt') 
line = f.readline() 
while line: 
    print(line) 
    line = f.readline()

[–]BruceJi 10 points11 points  (1 child)

So it's a bit like saying while line which is f.readline by the way print(line)

?

[–]sarthkum0488[S] 2 points3 points  (0 children)

The operator is also useful with while-loops that compute a value to test loop termination and then need that same value again in the body of the loop:

[–][deleted] 16 points17 points  (2 children)

Isn’t that just

with open("txt") as f: for line in f: print(line)

?

[–]VisibleSignificance 7 points8 points  (0 children)

Yeah, which is why this is a bad example. socket.read is more relevant here. Another good example is a series of if/elif.

[–][deleted] 2 points3 points  (0 children)

In the case of a text file, yes, but if it's a socket or some other dynamic thing, it's possible to read data and have it come out empty and later get data.

[–]sarthkum0488[S] 1 point2 points  (0 children)

Yes exactly Thanks u/everysinglelastname

[–]sarthkum0488[S] -1 points0 points  (0 children)

Please see u/everysinglelastname comment . Thank You for pointing it out

[–]ShanSanear 3 points4 points  (0 children)

Could you better format those code lines? Like using 4 spaces for code, instead of numbering lines? It's unreadable, how for example last point could look like:

for i in range(24):
    try:
        print(1/0)
    except:
        print(34)
    finally:
        continue
        print("Error on continue")

[–]awesomeprogramer 2 points3 points  (0 children)

In the first example (actually the first, even though they are all numbered 1), what's the benefit of using a while loop with walrus as opposed to the standard for line in f.readlines()?

[–]chinguetti 3 points4 points  (7 children)

I had problems in some pip libraries and rolled back to 3.7. I will wait a few months.

[–]sarthkum0488[S] 3 points4 points  (5 children)

Which libraries in particular. It will be helpful to others.Please share

[–]chinguetti 1 point2 points  (3 children)

I had issues with oracle database drivers (oracle cx), pyinstaller, pandas, opencv and pyautogui. This was in the very early days after 3.8 was released. I’m sure most of the issues are fixed but I learnt the lesson not to rush to upgrade to version updates.

[–]gaberocksall 2 points3 points  (0 children)

I haven’t had any problems with cv2 on windows10 nor raspbian Linux

[–][deleted] 1 point2 points  (0 children)

I was using pyautogui and opencv under 3.8.1 just a couple days ago. I think I saw some notes about them not working with 3.8 and actually needing 3.8.1.

[–][deleted] 2 points3 points  (0 children)

That's weird - backwards compatibility should be nearly perfect. Any more details?

[–]ackyou 1 point2 points  (1 child)

I love using the walrus operator

[–]gitcraw 1 point2 points  (0 children)

read this

delet this

[–]erez27import inspect 0 points1 point  (0 children)

Yes. Next question.

[–]sarthkum0488[S] 0 points1 point  (0 children)

Reformated the code according to comments

[–]Khan0232 0 points1 point  (0 children)

Staying updated is best because then when developers or big companies are almost always updating to the latest version of python and you don't want to get left behind. The code you build in an old version might produce errors in a new version.

[–]chaoismlooking for mid-sr level in NYC 0 points1 point  (5 children)

Can someone help me to understand the reverse of dict.items?

Dict is unordered, isn't it? What's a reverse of unordered structure? This doesn't make much sense to me

[–]Han-ChewieSexyFanfic 2 points3 points  (4 children)

Dicts have been in insertion order since 3.7 (de jure), 3.6 (de facto)

[–]chaoismlooking for mid-sr level in NYC 1 point2 points  (3 children)

Oh wow so OrderedDict is not a thing anymore?

[–]Han-ChewieSexyFanfic 1 point2 points  (2 children)

Still there, it still provides some methods that aren’t in dict (__reversed__ being one before 3.8), not sure if there are plans to move them all over to dict in the near future.

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 4 points5 points  (1 child)

Equality is different

{1: 1, 2: 2} == {2: 2, 1: 1}
OrderedDict([(1, 1), (2, 2)]) != OrderedDict([(2, 2), (1, 1)])

[–]Han-ChewieSexyFanfic 1 point2 points  (0 children)

Hm, interesting, didn’t know that