2

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?

Daniel
  • 3,344
  • 5
  • 27
  • 35
  • 1
    Why the hell does this code need to access `self.caller.parent.parent.parent.caller.parent.bar.ids.actionview`? This sounds like a horrible program structure. – user2357112 Oct 17 '16 at 00:43
  • @user2357112 Yeah I would prefer to find another way to do it but the way the kivy environment works some widgets such as popups have the main program window as the parent and not the object that created them and thus I haven't figured out a way to address widgets from within a function call on these separated objects. I need to dynamically create and destroy widgets as the user proceeds through the program and this has been the only solution I've come up with so far. That being said, I'm clearly new to OO programming and I'm sure there is a better way. – Daniel Oct 17 '16 at 00:48
  • `.` is still an operator. And you can always parenthesize the whole expression. As for preferred - well that's opinionated, but: a) do not write code like this, or b) if you have to, break it into some variables, with comments what is happening. – Antti Haapala -- Слава Україні Oct 17 '16 at 04:24

1 Answers1

1

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)
)
  • Thanks this works; as for readability, I agree this is really not ideal. Unfortunately I have deeply nested objects and the kivy environment I'm using (as far as I can tell) doesn't allow you to access objects by id unless they were defined in a static way within the .kv file. I'm creating and destroying objects dynamically and thus have to address them directly within the hierarchy. – Daniel Oct 17 '16 at 00:45
  • Still, off the top of my head, I can't even see how a *malicious* library would prevent you from something like `mybar = self.caller.parent.parent.parent.caller.parent.bar` and then using `mybar` from there. –  Oct 17 '16 at 00:52
  • Good point... I'll do that. – Daniel Oct 17 '16 at 00:54