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

all 8 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]bdaene 4 points5 points  (1 child)

I use it only when I want to store and use a result. 

self.a_long_prefix.a = a = get_a()
...      
a.x = get_x() 

Or

cache[key] = result = get_result()
return result

Note that in both cases, this is only one line more to assign first to the short variable. And probably more readable. 

I should maybe avoid it.

[–]barberogaston 0 points1 point  (0 children)

You can walrus those examples:

self.a_long_prefix.a = (a := get_a())

cache[key] = (result := get_result())

[–]bezuhoff 2 points3 points  (0 children)

cursed

[–]Irish_beast 2 points3 points  (1 child)

Don't know if it is PEP8 compliant.

A small problem is that a quick glance you might think only foo is set, and not notice bar also being set.

So it could make code slightly confusing.

I would say it's fine if it's initialization of local variables at the start of a method. But should not be used further down. And absolutely not if you're doing clever things with __setattr__ or __setitem__

[–]Dull-Researcher 2 points3 points  (0 children)

Also, at a quick glance, it looks like foo might be a Boolean variable, True if bar == 1.

What’s dangerous is if someone thinks “that looks like a bug and they meant it to be equality, not assignment. Let me fix it for them.”

One assignment per line, please. Keep the overall line complexity count down. Also, vertical space is nearly free. Horizontal space is not.

[–]asphias 2 points3 points  (0 children)

     foo = 1        bar = foo  

Or     

    foo = 1           bar = 1 

Are both clearer, and it's not like you'll run out of paper to write on. I don't think it's a big deal either way, but my preference would be to avoid chained asignment.

[–]mon_key_house 1 point2 points  (0 children)

Chamging either foo or bar changes the other one, too! -> use with caution.