by shigemk2

当面は技術的なことしか書かない

エキスパートPythonプログラミング デコレータ


# before 2.3
class WhatFor(object):
    def it(cls):
        print ('work with %s' % cls)
    it = classmethod(it) # make it classmethod

    def uncommon():
        print ('I could be a global function')
    uncommon = staticmethod(uncommon) # make it staticmethod

whatfor = WhatFor()
whatfor.it() # work with <class '__main__.WhatFor'>
whatfor.uncommon() # I could be a global function

# since 2.4

class WhatFor2(object):
    @classmethod
    def it(cls):
        print ('work with %s' % cls)

    @staticmethod
    def uncommon():
        print ('I could be a global function')

whatfor = WhatFor2()
whatfor.it() # work with <class '__main__.WhatFor2'>
whatfor.uncommon() # I could be a global function