Spread a statement over various lines

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Tue Sep 17 16:51:04 EDT 2019


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?



More information about the Python-list mailing list