convert function to class method?


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?



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...

def turn_matching_rules_into_where_clause(table_being_matched: Table, template: Template, matching_rules: []) -> str:

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.



BTW There is a feature request about this functionality -- PY-34363.


Please sign in to leave a comment.