Is this a PyCharm 2017.2.2 refactoring limitation, or a coding error?

164 views Asked by At

I'm a fan of JetBrains IntelliJ, and now am enjoying the support PyCharm offers for Python. But I'm having trouble trying to refactor some python code correctly with PyCharm 2017.2.2. I can demonstrate the problem with two cases, where the first one works but the second one doesn't.

Working snippet:

import pandas as pd

def main():
    c = 2
    a = pd.Series([1,2,3])
    b = a.apply(lambda x:
        x+1
    )
    print (b)

main()

I can select the three lines

    b = a.apply(lambda x:
        x+1
    )

When I refactor > extract > method, I'm offered a dialog box suggesting a as a parameter, and b as the return value. When I accept, the result is:

import pandas as pd

def main():
    c = 2
    a = pd.Series([1,2,3])
    b = method_name(a)
    print (b)


def method_name(a):
    b = a.apply(lambda x:
                x + 1
                )
    return b


main()

Now, I make a change. Instead of x+1 inside the lambda, I use x+c. I would expect that c would then be identified as a parameter for the new function, but it isn't. As a result, I end up with this code, which doesn't work:

import pandas as pd

def main():
    c = 2
    a = pd.Series([1,2,3])
    b = method_name(a)
    print (b)


def method_name(a):
    b = a.apply(lambda x:
                x + c
                )
    return b


main()

Is there something I'm doing wrong?

1

There are 1 answers

2
Thomas Myers On

Your only passing one value to method_name so c represents nothing.