Daniel Daniel - 1 month ago 7
Python Question

How to break long lines in python that have no brackets or operators?

This is different from similar questions I have found on this site as the code in question doesn't have any operators or brackets before the character limit to split easily on. I have several long lines in python without operators or brackets before 79 characters. As an example:

self.caller.parent.parent.parent.caller.parent.bar.ids.actionview.remove_widget(self.caller.parent.parent.parent.caller.parent.bar.ids.actionview.startbutton)


The above line has 72 characters before a bracket, and is within a function definition within a class so therefore with 4-char spacing per nest level, has 81 characters before a bracket.

What is the preferred way of dealing with this?

Answer

With your particular example, I expect if you first solved the problem of "make this code readable", fitting in 79 characters would come naturally.

That said, you can add brackets:

(self.caller.parent.parent
 .parent.caller.parent.bar
 .ids.actionview.remove_widget(
    self.caller.parent.parent
    .parent.caller.parent
    .bar.ids.actionview.startbutton)
)
Comments