convert function to class method?
Answered
I see there is the inverse option to turn a method into a function. Is there a way to refactor a function so that it becomes a method of a class?
Please sign in to leave a comment.
Hi Marcel, sorry for the late reply.
No, unfortunately, there is no such feature at the moment. The obvious way to create such methods is to turn them into stateless @staticmethods, but the latter don't seem to be used that frequently in Python as e.g. in Java. Probably, that's the reason why this functionality hasn't been considered before.
Just to add a reason for this... in my case, the function receives the object into which I want to move the function. It then works on that instance of the object. I'd rather just have the function be a method in that object allowing me to just work on the object directly.
Here is my current function signature...
table_being_matched is really just a variable in my TBL class. So, I'd rather just make this a method of my TBL class.
Ben
BTW There is a feature request about this functionality -- PY-34363.