0

I have

return "Cargo(location=%s, cargocapacity=%s, cargo=%s, name=%s, mast_count=%s)" \
        % (
            self.location,
            self.cargocapacity,
            self.cargo,
            self.name,
            self.mast_count)

so the first line is too long. How can i break the Line? If i do

return "Cargo(location=%s, cargocapacity=%s, cargo=%s, \
       name=%s, mast_count=%s)" \
        % (
            self.location,
            self.cargocapacity,
            self.cargo,
            self.name,
            self.mast_count)

and print the returned string i get a lot of withespace.

Is there a way to do this? Maybe join the string with single arguments? But this will add a lot more lines of code.

Finn
  • 1,999
  • 2
  • 24
  • 29
  • 1
    consider using templating. `template = "Cargo(location={location}, cargocapacity={cargocapacity}, cargo={cargo}, name={name}, mast_count={mast_count})"; return template.format(self)` – wim Dec 08 '14 at 15:44
  • Note also that you probably want the `repr`, rather than `str`, forms of the attributes. – jonrsharpe Dec 08 '14 at 15:51

2 Answers2

2

Putting strings together "like " "this" will join them into one, "like this". It can be used across lines.

return ("Cargo(location=%s, cargocapacity=%s, cargo=%s, "
        "name=%s, mast_count=%s)") % (
        self.location,
        self.cargocapacity,
        self.cargo,
        self.name,
        self.mast_count)
Kevin
  • 28,963
  • 9
  • 62
  • 81
1

You can split up a string between parentheses and have Python automatically join them at compilation time:

return (
    "Cargo(location=%s, cargocapacity=%s, cargo=%s, "
    "name=%s, mast_count=%s)") % (
            self.location,
            self.cargocapacity,
            self.cargo,
            self.name,
            self.mast_count)

The parentheses are not really required if you use \ before the end of the line, but with parentheses this is just a little cleaner.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • [\ is discouraged in PEP-8](https://www.python.org/dev/peps/pep-0008/#maximum-line-length), because trailing whitespace invisibly breaks it. In some cases, the resulting syntax is actually legal, so you get entirely wrong behavior with no obvious reason why. – Kevin Dec 08 '14 at 15:42
  • @Kevin: if you have a long `with` statement, the only option to break it across multiple lines is to use backslashes. But you are right, PEP 8 discourages their use, for good reasons. – Martijn Pieters Dec 08 '14 at 15:44
  • Sure, but I'd prefer to use [`contextlib.ExitStack`](https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack) there anyway. – Kevin Dec 08 '14 at 15:45
  • @Kevin: which is not available in Python 2.. – Martijn Pieters Dec 08 '14 at 15:46
  • Lots of stuff isn't in Python 2. That's why I prefer Python 3. – Kevin Dec 08 '14 at 15:48
  • @Kevin: sure, if you have the choice; whenever I can I make that same choice. But loads of developers cannot yet make that switch. If you are targeting Google App Engine you also don't get to choose, etc. – Martijn Pieters Dec 08 '14 at 15:49
  • contextlib.ExitStack has a backport -> http://contextlib2.readthedocs.org/en/latest/ – wim Dec 08 '14 at 15:59
  • @wim: sure, but a strategically-placed backslash on a wide `with` line is fine; installing a backport is overkill there. – Martijn Pieters Dec 08 '14 at 16:00
  • I agree, just an fyi for any future readers – wim Dec 08 '14 at 16:05