python - should I use static methods or top-level functions

后端 未结 5 1368
小蘑菇
小蘑菇 2020-12-23 14:01

I come from a Java background and I\'m new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing file

5条回答
  •  既然无缘
    2020-12-23 14:46

    If the function is related to a class, then make it a static method. For example:

    class DBobject():
        name = StringProperty()
    
        @staticmethod
        def get_by_name(name):
            return db.select('select * from DBobject where name = "%s"' % name)
    
    python = DBobject.get_by_name('python')
    

    That is, the method is completely related to the DBobject class so it should be a static method.

提交回复
热议问题