What is a good way to order methods in a Python class?

前端 未结 3 586
不知归路
不知归路 2020-12-12 16:13

I want to order methods in a Python class but I don\'t know what is the correct order.

When I extract methods in Eclipse with PyDev, Eclipse puts the extracted metho

3条回答
  •  悲哀的现实
    2020-12-12 16:52

    I do something similar to @Ethan that I saw in Django's source, where the main difference is big "############" block comments to delimit the areas. For example,

    class SomeClass(object):
        #################
        # Magic Methods #
        #################
        def __magic_methods__(self):
            "magic methods first"
    
        ##################
        # Public Methods #
        ##################
        def a_method(self):
            "then normal methods, in order of importance"
    
        ###################
        # Private Methods #
        ###################
        def _private_method(self):
            "then worker methods, grouped by importance or related function"
    

    Obviously this is less useful for smaller classes.

提交回复
热议问题