Inelegant

D

Dan

I've just begun playing with Python, and so far I like what I see.
It's a very elegant language. But I've found something that's, well,
a bit ugly. Maybe someone can point out to me where I'm wrong.

If you use triple quotes to define a string, then the newlines are
implicitly included. This is a very nice feature. But if you're
inside a function or statement, then you'll want the string to be
positioned along that indentation. And the consequences of this is
that the string will include those indentations.

For example:

def SomeFunction()
if SomeCondition:
MyString =
"""
The quick brown fox
"""
print MyString

The output will be:

The quick brown fox

But what you really want is:

The quick brown fox

The way around it is to write the function thus:

def SomeFunction()
if SomeCondition:
MyString =
"""
The quick brown fox
"""
print MyString

But that's just ugly. It seems to me that the string should be
interpreted with the edge along the indentation line, not from the
start of the line. But that would probably create other problems.

Dan
 
T

Terry Hancock

If you use triple quotes to define a string, then the newlines are
implicitly included. This is a very nice feature. But if you're
inside a function or statement, then you'll want the string to be
positioned along that indentation. And the consequences of this is
that the string will include those indentations.
[...]
But that's just ugly.

Yeah, it's definitely a wart. So much so that recent Python
distributions include a function to fix it:
.... This string will have the leading
.... spaces removed so that it doesn't
.... have to break up the indenting.
.... """)
This string will have the leading
spaces removed so that it doesn't
have to break up the indenting.
 
B

Bengt Richter

If you use triple quotes to define a string, then the newlines are
implicitly included. This is a very nice feature. But if you're
inside a function or statement, then you'll want the string to be
positioned along that indentation. And the consequences of this is
that the string will include those indentations.
[...]
But that's just ugly.

Yeah, it's definitely a wart. So much so that recent Python
distributions include a function to fix it:
... This string will have the leading
... spaces removed so that it doesn't
... have to break up the indenting.
... """)
This string will have the leading
spaces removed so that it doesn't
have to break up the indenting.

I never liked any of the solutions that demand bracketing the string with expression brackets,
but I just had an idea ;-)
... def __init__(self, **kw): self.kw = kw
... def __add__(self, s):
... lines = s.splitlines()[1:]
... margin = self.kw.get('margin', 0)*' '
... mnow = min(len(L)-len(L.lstrip()) for L in lines)
... return '\n'.join([line[mnow:] and margin+line[mnow:] or '' for line in lines])
...
...

Normally you wouldn't pass **kw in like this,
you'd just write
mystring = Dedent()+\

or

mystring = Dedent(margin=3)+\

but I wanted to control the print. Note the the first line, unless you escape it (ugly there)
is zero length and therefore has zero margin, which subverts the other shifting, so I just drop that line.
You have to live with the backslash after the + as payment for preferring not to have parentheses ;-)
... mystring = Dedent(**kw)+\
... """
... This makes
... for a cleaner isolation
... of the string IMO.
... """
... return mystring
... ----
This makes
for a cleaner isolation
of the string IMO.
---- ----
This makes
for a cleaner isolation
of the string IMO.
----

Regards,
Bengt Richter
 
G

gry

I sometimes use the implicit literal string concatenation:

def SomeFunction():
if SomeCondition:
MyString = 'The quick brown fox ' \
'jumped over the ' \
'lazy dog'
print MyString

SomeFunction()
The quick brown fox jumped over the lazy dog


It looks pretty good, I think. One could use triple quotes too, if the
string
contains quotes.

-- George
 
G

Greg Ewing

Bengt said:
I never liked any of the solutions that demand bracketing the string with expression brackets,
but I just had an idea ;-)

Or for an even more twisted idea:


from textwrap import dedent

class _Dedent(type):

def __new__(cls, name, bases, dict):
if name == "*": # for bootstrapping
return type.__new__(cls, name, bases, dict)
return dedent(dict['__doc__'])

DedentedString = _Dedent("*", (), {})

#
# Usage example
#

class foo(DedentedString):
"""
This is a dedented (or perhaps demented?) string.
It spans multiple lines.
"""

print type(foo)
print foo

The output is:

<type 'str'>

This is a dedented (or perhaps demented?) string.
It spans multiple lines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,234
Messages
2,571,180
Members
47,813
Latest member
RustyGary3

Latest Threads

Top