0

I saw a function written like this:

def transform(self, data):
    mtdReturnData =  data. \
        rename('MTDReturn', 'TimeSeriesValue'). \
        addfield('FundID', 7). \
        addfield('TimeSeriesTypeID', 38)

    # remaining step is to figure out formular for last price
    lastPriceData = mtdReturnData. \
        rename('TimeSeriesValue', 'MTDReturn'). \
        min_date = min(df['EffectiveDate']). \
        addfield('TimeSeriesValue', lambda x: x['MTDReturn'] * 2). \
        cutout('MTDReturn'). \
        convert('TimeSeriesTypeID', 12)

    return mtdReturnData, lastPriceData

Can't really understand what the . \ does, does it simply create a new line to chain methods?

  • 4
    Yes, the backslash is a line continuation, and has nothing in particular to do with the preceding `.` – Charles Duffy Jun 07 '18 at 21:22
  • 2
    On a side note: What a horrible, horrible style. No wonder the author had to resort to backslashes to make the code at least a bit readable. – zwer Jun 07 '18 at 21:31
  • 1
    This looks a good example--or rather an example of bad style--that shows why [PEP 8](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) says to avoid using the backslash continuation character as much as possible. Better to wrap the entire thing in parentheses and use Python's implied line continuation inside parentheses, brackets and braces. Better yet to write in such a way as to avoid the issue. – Rory Daulton Jun 07 '18 at 21:32
  • This style is very common when using a _fluent_ API. In a fluent API, setters will end with `return self` (or sometimes `return a copy of self with the attribute updated`), so that you can chain multiple method calls as part of an object construction or assignment. There is not yet a consensus on whether this is actually Pythonic style (as demonstrated by other commentors). This idiom traces at least back to Smalltalk, where nearly every method ended with `^self` (or `return self` in Python) to support this form of method chaining. – PaulMcG Jun 07 '18 at 21:33
  • so fluent it can't work `min_date = min(df['EffectiveDate']). \ ` edit: well maybe it can. but oh noes – bobrobbob Jun 07 '18 at 22:15
  • Possible duplicate of [How can I do a line break (line continuation)?](https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation) – bobrobbob Jun 07 '18 at 22:21

0 Answers0