六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 77|回复: 0

Amazing Python 3: "@"

[复制链接]

升级  76%

48

主题

48

主题

48

主题

秀才

Rank: 2

积分
164
 楼主| 发表于 2013-2-7 15:12:27 | 显示全部楼层 |阅读模式
1. This an interesting usage that I don't know how to call it.  See example:
def hehe(tt):        return 'hehe'+tt()@hehedef test():    return 'test'print test 
The output is same as:
def hehe(tt):        return 'hehe'+tt()def test():    return 'test'test = hehe(test)print test  
Output:
hehetest 
P.S: It enables such an easy way to invoke functions by strings! - cloud 
 
From http://www.pythonid.com/html/fenleiwenzhang/lang/20070910/185.html
 
2. Use "@staticmethod" to realize static method in Python (Python does not have keyword "static"):
class AClass():    @staticmethod     def astatic():    # pay attention that there's no "self" here!        print 'It's a static method!'AClass.astatic()    
Output:
It's a static method! 
By the way, there's another way to make a method static with a same output:
class AClass():    def aclassmethod():         print 'It's a static method!'    aclassmethod = classmethod(aclassmethod)   # you can write "whatevername = classmethod(aclassmethod)" after Python2.5  
 
From http://wiki.woodpecker.org.cn/moin/PythonEssentialRef7
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表