2

My IDE keeps suggesting I convert my instance methods to static methods. I guess because I haven't referenced any self within these methods.

An example is :

class NotificationViewSet(NSViewSet):

    def pre_create_processing(self, request, obj):
        log.debug(" creating messages ")

        # Ensure data is consistent and belongs to the sending bot.
        obj['user_id'] = request.auth.owner.id
        obj['bot_id'] = request.auth.id

So my question would be: do I lose anything by just ignoring the IDE suggestions, or is there more to it?

3
  • Um that's a smart IDE. Which one are you using? Commented Jan 26, 2015 at 11:47
  • 1
    @Roberto I know pycharm has this facility. Commented Jan 26, 2015 at 11:55
  • 1
    @Roberto I confirm that it is pycharm Commented Jan 26, 2015 at 14:08

2 Answers 2

2

This is a matter of workflow, intentions with your design, and also a somewhat subjective decision.

First of all, you are right, your IDE suggests converting the method to a static method because the method does not use the instance. It is most likely a good idea to follow this suggestion, but you might have a few reasons to ignore it.

Possible reasons to ignore it:

  1. The code is soon to be changed to use the instance (on the other hand, the idea of soon is subjective, so be careful)
  2. The code is legacy and not entirely understood/known
  3. The interface is used in a polymorphic/duck typed way (e.g. you have a collection of objects with this method and you want to call them in a uniform way, but the implementation in this class happens to not need to use the instance - which is a bit of a code smell)
  4. The interface is specified externally and cannot be changed (this is analog to the previous reason)
  5. The AST of the code is read/manipulated either by itself or something that uses it and expects this method to be an instance method (this again is an external dependency on the interface)

I'm sure there can be more, but failing these types of reasons I would follow the suggestion. However, if the method does not belong to the class (e.g. factory method or something similar), I would refactor it to not be part of the class.

Sign up to request clarification or add additional context in comments.

Comments

1

I think that you might be mixing up some terminology - the example is not a class method. Class methods receive the class as the first argument, they do not receive the instance. In this case you have a normal instance method that is not using its instance.

If the method does not belong in the class, you can move it out of the class and make it a standard function. Otherwise, if it should be bundled as part of the class, e.g. it's a factory function, then you should probably make it a static method as this (at a minimum) serves as useful documentation to users of your class that the method is coupled to the class, but not dependent on it's state.

Making the method static also has the advantage this it can be overridden in subclasses of the class. If the method was moved outside of the class as a regular function then subclassing is not possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.