Ask Your Question
2

After checking for None type, is it possible to refactor multiple assignments?

asked 2022-08-28 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-08-30 11:00:00 +0000

nofretete gravatar image

Yes, it is possible to refactor multiple assignments after checking for None type. Here is an example:

if my_variable is not None:
    a = my_variable.a
    b = my_variable.b
    c = my_variable.c

This can be refactored using tuple unpacking like this:

if my_variable is not None:
    a, b, c = my_variable.a, my_variable.b, my_variable.c

This way, the code is more concise and readable.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-28 11:00:00 +0000

Seen: 13 times

Last updated: Aug 30 '21