tweaking @decorator syntax

Sandy Norton sandskyfly at hotmail.com
Thu Aug 5 10:41:57 CEST 2004


On 4 Aug 2004, Christopher T King wrote:
    
> Of course, I prefer the nested block idea better (either with a new
> keyword or new syntax), but I don't see those (especially the syntax one) 
> flying anytime soon.

I know further discussion of this topic is feeling futile, but one can only hope.

> Unless someone can come up with an idea everyone can agree on Real Soon 
> Now, I think the whole idea should be dropped and wait until 3.0.  We'll 
> have plenty of time to argue about it then.

Agreed.

> Sorry about the long rant, but it felt good to get that all off my chest.

Please, we need more rants like yours. Now if they could somehow 
collectively become a 'public outcry' (-;

I realize my hasty initial post didn't actually show the present 2.4 form, 
so I've included it and added your variations for the sake of comparison:


Current 2.4b Syntax
-------------------

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

    @funcattrs(name='GvR', language='python')
    @log(file='func.log')
    def saygoodbye(self):
        print 'goodbye python world'        


Nested Alternative 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 'goodbye python world'        



Nested Alternative 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 'goodbye python world'        


Perhaps, you can eliminate the '@' alltogether. This is
definitely my favourite as it reads the way I think:

Nested Alternative 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 'goodbye python world'        

Alternative 4 (C T King)
------------------------

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

Alternative 5 (C T King)
------------------------

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


Methinks issues arise generally with the nested form 
in the case of:
    - v. long functions
    - using @decorators for annotating type info e.g:
    
        @accepts(int, str)
        @returns(str)
        def func(x, s):
            return s + str(x) 

        But I didn't think that this was the purpose of introducing func 
        decorators in the first place. 
        
        Hey since we are in amateur language design mode , 
        what the heck, let's play with that problem too: 
    
        haskell-like:
            
            int, str -> str
            def funct(x, s):
                return s + str(x) 
            
        boo-like:
    
            def func(x as int, s as str) as str :
                return s + str(x)
                
        a mix:
    
            def func(x as int, s as str) -> str:
                return s + str(x)
        
        examples of excessive colonic punctuation: 
            
            def func(x:int, s:str):
                return (s+str(x)):str
    
            def func(x:int, s:str):str :
                return s+str(x)
    
            def func str:(int:x, str:s):
                return s+str(x)
    
            def func(x:int, s:str):
                return (s+str(x)):str


Sheesh... I'm feeling nauseous and dizzy now so I think I'd better stop
before I crap on my keyboard. 

Language design land is no place for amateurs with feeble tummies.

ciao

Sandy



More information about the Python-list mailing list