rope icon indicating copy to clipboard operation
rope copied to clipboard

Convert a Function to a Method Feature Request

Open galvinograd opened this issue 9 years ago • 3 comments

Hey,

I think a lot of developers would benefit from the ability to move an arbitrary function to a class as a method, and break it down inside the class, in addition one should be able to choose to one of the following behaviors:

  • the class is initialized by an arg-less constructor
  • the class is initialized by a factory method
  • the method is a staticmethod
  • the method is a classmethod

Reference: http://refactoring.com/catalog/replaceMethodWithMethodObject.html

What do you think? (I'm more than willing to implement this feature :)

Additional acceptance criteria:

  • [ ] update documentation

galvinograd avatar Aug 01 '15 15:08 galvinograd

Gal Vinograd [email protected] wrote:

I think a lot of developers would benefit from the ability to move an arbitrary function to a class as a method, and break it down inside the class, in addition one should be able to choose to one of the following behaviors:

  • the class is initialized by the ctor
  • the class is initialized by a factory method
  • the method is a staticmethod
  • the method is a classmethod

What do you think? (I'm more willing to implement this feature :)

It does seem interesting. I wonder how much of this can be done right now with the inline method refactoring. For instance:

  class C(object):
      pass

  def func(a):
      return a * 2

Can be transformed into

  class C(object):
      @staticmethod
      def func(a):
          return a * 2

  def func(a):
     C.func(a)

And performing inline method on the global func().

Ali

aligrudi avatar Aug 01 '15 17:08 aligrudi

It sounds like the right way, I'll start implementing in the next few days :)

galvinograd avatar Aug 01 '15 21:08 galvinograd

Closing as duplicate of #371.

Edit: Hmmm... actually, on a second read, this doesn't seem to be a duplicate.

lieryan avatar Sep 08 '21 19:09 lieryan