tweaking @decorator syntax

Sandy Norton sandskyfly at hotmail.com
Thu Aug 5 19:04:00 EDT 2004


Here are some more variations.

Please copy/paste the code below into your favourite python editor and see 
how each syntactical variation of the decorator code reads, and how quickly 
you grok its structure and meaning.


#----------------------------------------------------------------------
# current syntax 2.3 syntax
#----------------------------------------------------------------------

class Klass:
    def __init__(self, name):
        '''class initialization
        '''
        self.name = name


    def statmethod1(x):
        '''does something to x
        '''
        return x
    statmethod1 = staticmethod(statmethod1)
 
    def statmethod2(y):
        '''does something to y
        '''
        return y
    statmethod2 = staticmethod(statmethod2)


    def clsmethod1(cls):
        '''does something to cls
        '''
        return cls
    clsmethod1 = classmethod(clsmethod1)

    def clsmethod2(cls):
        '''does something to cls
        '''
        return cls
    clsmethod2 = classmethod(clsmethod2)


    def go(obj):
        '''synced obj is activated
        '''
        obj.activate()
    go = synchronized(go)
    

    def f(x,y):
        '''tests two ints for equality
        '''
        return x == y
    f = accepts(f, (int, int))
    f = returns(f, (str))
        



#----------------------------------------------------------------------
# @decorator syntax proposed for 2.4 
#----------------------------------------------------------------------

class Klass:
    def __init__(self, name):
        '''class initialization
        '''
        self.name = name
        
    @staticmethod
    def statmethod1(x):
        '''does something to x
        '''
        return x

    @staticmethod    
    def statmethod2(y):
        '''does something to y
        '''
        return y
            
    @classmethod
    def clsmethod1(cls):
        '''does something to cls
        '''
        return cls

    @classmethod
    def clsmethod2(cls):
        '''does something to cls
        '''
        return cls

    @synchronized
    def go(obj):
        '''synced obj is activated
        '''
        obj.activate()
    
    @accepts(int, int)
    @returns(bool)
    def f(x,y):
        '''tests two ints for equality
        '''
        return x == y



#----------------------------------------------------------------------
# Nested, @decorator syntax proposed for 2.4 
#----------------------------------------------------------------------

class Klass:
    def __init__(self, name):
        '''class initialization
        '''
        self.name = name
        
    @staticmethod
        def statmethod1(x):
            '''does something to x
            '''
            return x
    
        def statmethod2(y):
            '''does something to y
            '''
            return y
            
    @classmethod
        def clsmethod1(cls):
            '''does something to cls
            '''
            return cls
    
        def clsmethod2(cls):
            '''does something to cls
            '''
            return cls

    @synchronized
        def go(obj):
            '''synced obj is activated
            '''
            obj.activate()
    
    @accepts(int, int)
    @returns(bool)
        def f(x,y):
            '''tests two ints for equality
            '''
            return x == y


#----------------------------------------------------------------------
# Nested, minimal decorator syntax proposed for 2.4 
#----------------------------------------------------------------------

class Klass:
    def __init__(self, name):
        '''class initialization
        '''
        self.name = name
        
    staticmethod:
        def statmethod1(x):
            '''does something to x
            '''
            return x
    
        def statmethod2(y):
            '''does something to y
            '''
            return y
            
    classmethod:
        def clsmethod1(cls):
            '''does something to cls
            '''
            return cls
    
        def clsmethod2(cls):
            '''does something to cls
            '''
            return cls

    synchronized:
        def go(obj):
            '''synced obj is activated
            '''
            obj.activate()
    
    accepts(int, int), returns(bool):
        def f(x,y):
            '''tests two ints for equality
            '''
            return x == y


#----------------------------------------------------------------------
# Nested, 'decorate' keyword syntax proposed for 2.4 
#----------------------------------------------------------------------

class Klass:
    def __init__(self, name):
        '''class initialization
        '''
        self.name = name
        
    decorate staticmethod:
        def statmethod1(x):
            '''does something to x
            '''
            return x
    
        def statmethod2(y):
            '''does something to y
            '''
            return y
            
    decorate classmethod:
        def clsmethod1(cls):
            '''does something to cls
            '''
            return cls
    
        def clsmethod2(cls):
            '''does something to cls
            '''
            return cls

    decorate synchronized:
        def go(obj):
            '''synced obj is activated
            '''
            obj.activate()
    
    decorate accepts(int, int), returns(bool):
        def f(x,y):
            '''tests two ints for equality
            '''
            return x == y
    

#----------------------------------------------------------------------
# Nested, |decorator| syntax proposed for 2.4 
#----------------------------------------------------------------------

class Klass:
    def __init__(self, name):
        '''class initialization
        '''
        self.name = name
        
    |staticmethod|
        def statmethod1(x):
            '''does something to x
            '''
            return x
    
        def statmethod2(y):
            '''does something to y
            '''
            return y
            
    |classmethod|
        def clsmethod1(cls):
            '''does something to cls
            '''
            return cls
    
        def clsmethod2(cls):
            '''does something to cls
            '''
            return cls

    |synchronized|
        def go(obj):
            '''synced obj is activated
            '''
            obj.activate()
    
    |accepts(int, int), returns(bool)|
        def f(x,y):
            '''tests two ints for equality
            '''
            return x == y



More information about the Python-list mailing list