tweaking @decorator syntax

Sandy Norton sandskyfly at hotmail.com
Wed Aug 4 18:09:43 CEST 2004


If we are going to be stuck with @decorators for 2.4, then how about
using blocks and indentation to elminate repetition and increase
readability:


Example 1
---------

class Klass:
    def __init__(self, name):
        self.name = name
    
    @staticmethod
        def statmethod1(x):
            return x
        
        def statmethod2(y):
            return y
            
    @classmethod
        def classmethod1(cls):
            return cls
        
        def classmethod2(cls):
            return cls
    
    @funcattrs(name='GvR', language='python')
    @log(file='func.log')
        def sayhello(self):
            print 'hello python world'
        
        def saygoodbye(self):
                    print 'goodby python world'        

Example 2
---------

class Klass:
    def __init__(self, name):
        self.name = name
    
    @staticmethod:
        def statmethod1(x):
            return x
        
        def statmethod2(y):
            return y
            
    @classmethod:
        def classmethod1(cls):
            return cls
        
        def classmethod2(cls):
            return cls
    
    @funcattrs(name='GvR', language='python'), 
        log(file='func.log'):
        def sayhello(self):
            print 'hello python world'
        
        def saygoodbye(self):
                    print 'goodby python world'        


Perhaps, you can eliminate the '@' alltogether:

Example 3
---------

class Klass:
    def __init__(self, name):
        self.name = name
    
    staticmethod:
        def statmethod1(x):
            return x
        
        def statmethod2(y):
            return y
            
    classmethod:
        def classmethod1(cls):
            return cls
        
        def classmethod2(cls):
            return cls
    
    funcattrs(name='GvR', language='python'),
        log(file='func.log'):
        def sayhello(self):
            print 'hello python world'
        
        def saygoodbye(self):
                    print 'goodby python world'        
        

Sandy



More information about the Python-list mailing list