-1

I'm sure we're all familiar with PEP 8, and its specification for line length. Unfortunately, mathematics exists.

I am trying to write REALLY long equations in Python (Trig, Surface area of complex shapes, etc.) and I would like to be able to retain clarity in my formulas without breaking up my lines too much.

For example, this formula is pretty long, and might be indented many times in my code, leaving me not much room to work with:

    slantHeight = ((math.sqrt(((baseSideLength/2) * (baseSideLength/2)) + heightSquared))

What's the recommended way to break up this line?

Will Nilges
  • 160
  • 3
  • 12
  • Can you explain what your use case is? Maybe there is another technique to get what you want. – Vincent Ramdhanie May 17 '16 at 01:07
  • 1
    What exactly do you want to mark? – kindall May 17 '16 at 01:08
  • 4
    [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – TigerhawkT3 May 17 '16 at 01:10
  • Do you mean a throwaway variable name [like _](http://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) ? – Treefish Zhang May 17 '16 at 01:12
  • 1
    Not without changing the language and the interpreter. – martineau May 17 '16 at 01:34
  • you want the language called brainf**k. – YOU May 17 '16 at 01:51
  • I think the answer to the questio nis "no" -- there is no "meaningless character" in python. Everything in a python script has meaning. – Bryan Oakley May 19 '16 at 12:33
  • It's unclear what you're trying to "mark" with them. Just separate out some parts of an expression for readability? Or they need to be automatically analyzed or something? – ivan_pozdeev May 19 '16 at 12:34
  • I think you are asking for a "midline comment" in which case this is a duplicate of http://stackoverflow.com/questions/5617159/mid-line-comment-in-python – Tadhg McDonald-Jensen May 19 '16 at 12:41
  • Otherwise the least i can say is: spread it out over multiple lines, align the parts of the equation with the open bracket on the previous line. Add comments at the end of each line to describe what it is doing. Other then the `#` everything has meaning in python or results in a syntax error. For good reason! – Tadhg McDonald-Jensen May 19 '16 at 12:47
  • If `?` was the mystery symbol, how would having it in your expression like you've shown help you? – Alex Hall May 21 '16 at 15:30
  • So, this question was written when I was brand new at programming, and wasn't even aware of the existence of PEP 8. That said, four-ish years later, I have edited this question to better fit the fundamentals behind Pythonic code structure, and make it more useful than, Q: "Does this exist?" A: "No!" – Will Nilges Jun 23 '20 at 22:49

1 Answers1

1

You can do this:

slantHeight = math.sqrt(
    (  # halfBaseSideLengthSquared
        (  # halfBaseSideLength
            baseSideLength / 2) *
        (baseSideLength / 2)) +
    (  # heightSquared
        height * height))

Or this:

def _(name, x):
    return x

slantHeight = math.sqrt(_('halfBaseSideLengthSquared', _('halfBaseSideLength', baseSideLength / 2) * (baseSideLength / 2)) + _('heightSquared', height * height))

You can even remove the name argument since it's ignored, though I can't see how this'll help with anything.

I recommend you stop worrying so much about file size and focus on readability. Extra lines will not hurt. Something like this:

halfBaseSideLength = (.5 * baseSideLength)
halfBaseSideLengthSquared = (halfBaseSideLength * halfBaseSideLength)

is useful because it reduces duplication, although the better thing to do would be:

halfBaseSideLengthSquared = (.5 * baseSideLength) ** 2
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • However many years later, I come back to this question after looking through the godawful source code that it pertained to. Turns out, I just needed to write more readable code! Imagine that. – Will Nilges Jun 23 '20 at 21:09