Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break a long assignment into two lines in Python [duplicate]

Tags:

python

pep8

I have the following code:

This.is.a.supper.long.name = This.is.another.supper.long.name

Currently the only method I know is to escape the newline:

This.is.a.supper.long.name = \
    This.is.another.supper.long.name

Is there any elegant solution to this?

like image 273
Huazuo Gao Avatar asked Apr 10 '15 07:04

Huazuo Gao


1 Answers

There are several approaches.

 common = This.is
 common.a.supper.long.name = common.another.supper.long.name

If that doesn't help, you can

 name = This.is.another.supper.long.name
 This.is.a.supper.long.name = name

or

 setName(This, getName(This))

where the methods allow to hide the long access path. This becomes even more readable when you can change This:

 This.setName(This.getName())
like image 199
Aaron Digulla Avatar answered Sep 29 '22 22:09

Aaron Digulla