OpenCV вблизи контуров [дубликат]

Чтобы решить, следует ли использовать @staticmethod или @classmethod , вы должны заглянуть внутрь своего метода. Если ваш метод обращается к другим переменным / методам в вашем классе, используйте @classmethod. С другой стороны, если ваш метод не касается каких-либо других частей класса, используйте @staticmethod.

class Apple:

    _counter = 0

    @staticmethod
    def about_apple():
        print('Apple is good for you.')

        # note you can still access other member of the class
        # but you have to use the class instance 
        # which is not very nice, because you have repeat yourself
        # 
        # For example:
        # @staticmethod
        #    print('Number of apples have been juiced: %s' % Apple._counter)
        #
        # @classmethod
        #    print('Number of apples have been juiced: %s' % cls._counter)
        #
        #    @classmethod is especially useful when you move your function to other class,
        #       you don't have to rename the class reference 

    @classmethod
    def make_apple_juice(cls, number_of_apples):
        print('Make juice:')
        for i in range(number_of_apples):
            cls._juice_this(i)

    @classmethod
    def _juice_this(cls, apple):
        print('Juicing %d...' % apple)
        cls._counter += 1

59
задан Harriv 8 March 2013 в 13:15
поделиться

3 ответа

95
ответ дан orangepips 26 August 2018 в 05:36
поделиться
43
ответ дан Abid Rahman K 26 August 2018 в 05:36
поделиться
1
ответ дан Daniel 26 August 2018 в 05:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: