Automatic refactoring of function parameters on class attributes
Hello, I'm new to PyCharm)
I have this question. Suppose I have a function with parameters:
def func(param1, param2, param3, param4, param5):
# do something with this params. It is a really long code.
# but for example we suppose it will be
return param1 + param2 + param3 + param4 + param5
And then I suddenly realized, the parameters are a single entity and it would be convenient to make a class that will have parameters.
class MyClass(object):
"""docstring"""
def __init__(self, param1, param2, param3):
"""Constructor"""
self.param1 = param1
self.param2 = param2
self.param3 = param3
Is there any special automatic refactoring to replace the parameters:param1, param2, param3 in the function code to MyClass attributes?
To replace all
param1 --> MyClass_obj.param1
param2 --> MyClass_obj.param2
param3 --> MyClass_obj.param3
in our code.
To get this result
def func(MyClass_obj, param4, param5):
# do something with this params
# for example
return MyClass_obj.param1 + MyClass_obj.param2 + MyClass_obj.param3 + param4 + param5
or here is such a "preparatory result 2"
def func(MyClass_obj.param1, MyClass_obj.param2, MyClass_obj.param3, param4, param5):
# do something with this params
# for example
return MyClass_obj.param1 + MyClass_obj.param2 + MyClass_obj.param3 + param4 + param5

I tried to do the usual rename refactoring, of course it detected an error in the name(the presence of a dot from the class) and did not allow me to do this.
Is it possible to implement such refactoring automatically in PyCharm?
Now I can only do this with the help of Multiple selections.
I'm moving my function to a separate python file and typing by Multiple Cursors in all param1's occurrences.
param1 --> MyClass_obj.param1
Then I do the same with another parameters: param2 and param 3.
I get the result "preparatory result 2" by this approach =) And then I do
But it seems to me that there is probably some special automatic refactoring for this purpose =)
Please sign in to leave a comment.
Hi,
There is no such refactoring and I don't think it is possible to implement. PyCharm has no way of knowing if those parameters are actually from some class.
Maybe I just miss something here so if you have any ideas on how PyCharm could handle such situations in a convenient and reliable way, please feel free to submit a feature request about it to our issue tracker using the link https://youtrack.jetbrains.com/issues/PY
Hello, Sergey )
Then I have an additional question ) And if it could be renamed using refactoring "param1" to "MyClass.param1"?
That is, if it were possible to make a renaming to a parameter with a dot? For example, with such refactoring, PyCharm would warn that you are renaming a parameter to an attribute of some class. Do you really want to do this?
And then PyCharm would just rename the parameter "param1" to "MyClass.param1", just as if we wanted to rename "param1" to "parameter1" ?
Then in semi manual mode we could rename
And then manually write it yourself
def func(param1,param2,param3,param4,param5)
to
def func(MyClass, param4, param5)
That would be very convenient )
That's probably a good way to handle such situations. There may be some pitfalls though.
Could you file a feature request about it to our issue tracker using the link https://youtrack.jetbrains.com/issues/PY?
This way other users will be able to vote for it and comment.
Thank you for your suggestion I will do it within two or three days )