def my_method(self):
print self.my_field * 2
I'd like to be able to add a parameter to this so that in some other case I can use a different expression instead of self.my_field, maybe self.my_other_field or self.my_field + 1. I select self.my_field and do Refactor > Extract > Parameter. I get an error saying "Cannot perform refactoring using selected element(s)".
It should be able to refactor, because my_method is called like self.my_method() or other_obj.my_method(). It can change these to self.my_method(self.my_field) or other_obj.my_method(other_obj.my_field).
In general I've only been able to get the extract parameter functionality to work when the expression is something very simple, like a constant. If it is an expression depending on the value of an existing parameter, it doesn't work. I guess self is a special case of an existing parameter. Is this just a limitation of extract parameter, or am I doing it wrong?
It seems like what you are actually trying to do is refactor through renaming as opposed to extracting a new parameter.
You will note that the intended function of parameter extraction is to modify existing calls to include your new parameter. However, based on what you stated in your question:
I select self.my_field
it would seem that you are either accidentally selecting the statement within your method, or attempting to modify the expression through refactoring. (Or, if something else, could you provide a clarification of your intended outcome?) In the latter case, that would be better suited for an alternative refactoring.
In general, refactoring you code through extraction is taking a piece of code:
def my_method():
return 1 + 2
and moving its innards elsewhere:
def my_method(a=1,b=2):
return a + b
In your snippet:
def my_method(self):
print(self.my_field * 2)
the body of the method is not inclusive and therefore it would not make sense to extract from the statement. Instead, it would seem that you are wanting to simply replace self.my_field with self.my_other_field within the declaration of my_method. Again, it is somewhat unclear due to the simplicity of your example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With