osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Spread a statement over various lines


On 9/17/19 2:59 PM, Manfred Lotz wrote:

 > def regex_from_filepat(fpat):
 >      rfpat = fpat.replace('.', '\\.') \
 >                        .replace('%', '.')  \
 >                        .replace('*', '.*')
 >
 >      return '^' + rfpat + '$'
 >
 > As I don't want to have the replace() functions in one line my
 > question is if it is ok to spread the statement over various lines as
 > shown above, or if there is a better way?

Is that way okay?  Sure.  Are there other ways?  Sure.

To isolate each replace() function on its own line, and to eliminate the
clutter of the backslashes, consider this:

     rfpat = (fpat
               .replace('.', '\\.')
               .replace('%', '.')
               .replace('*', '.*')
             )

"Better" is going to depend on your initial reason for not wanting all
those function calls on one line.  What is that reason?