Home > front end >  Is it okay to abuse @classmethod and @staticmethod In python?(Django)
Is it okay to abuse @classmethod and @staticmethod In python?(Django)

Time:07-13

I have used spring framework for developing server application, and now I start to learn Django.

I got one question when I use Django.

"Is there no issue about memory when using @classmethod or @staticmethod? in python"

In spring(java), there is issue about abusing static and spring also support controlling object(IoC container).

But when I use Django, there is no decorator or setting about object.

Just I use @classmethod, and use it with class name (ex. AccountService.join() )

CodePudding user response:

@statictmethod can be used if you need a method which you can call with ModelName.method_name() and which don't operate on the object.

You don't really use classmethod in your Models. These operations are done via a Manager class. See this accepted answer on classmethod Django model class methods for predefined values

CodePudding user response:

In "normal use", this is fine. Class methods do not get access to instance data, and static methods don't get access to the parent class. If these are not issues for you, then you can use @classmethod or @staticmethod respectively. In reality though, semantic/readability benefits aside, this is likely premature optimisation, especially in a web application context where the lion's share of latency/resources go to other things.

  • Related