`
cloudhe
  • 浏览: 107176 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Amazing Python 3: "@"

阅读更多

1. This an interesting usage that I don't know how to call it.  See example:

def hehe(tt):    
    return 'hehe'+tt()

@hehe
def 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

分享到:
评论

相关推荐

    amazing-qr::white_flower: amazing QR-Code generator in Python (supporting animated gif) - Python amazing 二维码生成器(支持 gif 动态图片二维码)

    内容 例子 安装# via pippip install amzqr 用法 终点航道(提示:如果尚未安装amzqr ,则应使用python(3) amzqr.py而不是amzqr blow。) # summaryamzqr Words [-v {1,2,3,...,40}] [-l {L,M,Q,H}] [-n output-...

    Python GUI Programming Cookbook.pdf 2015 0分

    Use object-oriented programming to develop amazing GUIs in Python Create a working GUI project as a central resource for developing your Python GUIs Packed with easy-to-follow recipes to help you ...

    Mastering Natural Language Processing with Python [2016]

    Maximize your NLP capabilities while creating amazing NLP projects in Python About This Book Learn to implement various NLP tasks in Python Gain insights into the current and budding research topics...

    The Quick Python Book 3rd

    Initially Guido van Rossum's 1989 holiday project, Python has grown into an amazing computer language. It's a joy to learn and read, and powerful enough to handle everything from low-level system ...

    Python GUI Programming Cookbook

    Use object-oriented programming to develop amazing GUIs in Python Create a working GUI project as a central resource for developing your Python GUIs Packed with easy-to-follow recipes to help you ...

    Python GUI Programming Cookbook Second Edition 2nd

    Use object-oriented programming to develop amazing GUIs in Python Create a working GUI project as a central resource for developing your Python GUIs Easy-to-follow recipes to help you develop code ...

    Coding project in python

    Perfect for kids ages 10 and over who are ready to take a second step after Scratch, Coding Projects in Python teaches kids how to build amazing graphics, fun games, and useful apps. All they need is ...

    Python Microservices Development

    A very useful guide for Python developers ... It's a practical book: you’ll build everything using Python 3 and its amazing tooling ecosystem. You will understand the principles of TDD and apply them.

    Amazing_maze:COSC 302的最终项目

    python3 start.py //have fun! 目前的进展 移动角色 产生随机迷宫 随机出现的星星 接起星星! 获得更高的分数 摧毁一堵随机的墙 增加敌人 4个难度等级 添加了开始菜单 游戏玩法 你 敌人 铲 提示钟 在游戏中,您...

    OpenCV with Python By Example

    Web developers can develop complex applications without having to reinvent the wheel., This book will walk you through all the building blocks needed to build amazing computer vision applications ...

    Cloud Native Python_Packt Publishing(2017).pdf

    we're going to build everything using Python 3 and its amazing tooling ecosystem. The book will take you on a journey, the destination of which is the creation of a complete Python application based ...

    flit:简化的Python模块包装

    安装$ python3 -m pip install flitFlit需要Python 3,因此需要使用Python 3版本的pip进行安装。 可以使用Flit分发Python 2模块,但需要在Python 3上无错误地导入。用法假设您正在编写模块foobar (作为单个文件...

    Coding.Projects.in.Python.2017.pdf

    Perfect for kids ages 10 and over who are ready to take a second step after Scratch, Coding Projects in Python teaches kids how to build amazing graphics, fun games, and useful apps. All they need is ...

    [machine_learning_mastery系列]Machine_Learning_Mastery_With_Python.pdf

    I think Python is an amazing platform for machine learning. There are so many algorithms and so much power ready to use. I am often asked the question: How do you use Python for machine learning? This...

    Building.RESTful.Python.Web.Services.epub

    based APIs from scratch Who This Book Is For This book is for web developers who have working knowledge of Python and would like to build amazing web services by taking advantage of the various ...

    Python进阶之自定义对象实现切片功能

    切片是 Python 中最迷人最强大最 Amazing 的语言特性(几乎没有之一),在《Python进阶:切片的误区与高级用法》中,我介绍了切片的基础用法、高级用法以及一些使用误区。这些内容都是基于原生的序列类型(如字符串...

    Building-Web-Applications-with-Flask-Use-Python-and-Flask-to-build-amazing-web-applications-just-the-way-you-want-them-. ....pdf

    Building-Web-Applications-with-Flask-Use-Python-and-Flask-to-build-amazing-web-applications-just-the-way-you-want-them-.pdf

    OpenCV With Python by Example

    Web developers can develop complex applications without having to reinvent the wheel., This book will walk you through all the building blocks needed to build amazing computer vision applications ...

    python-hand-book:我的python笔记

    Python手册 它是什么? python-hand-book是本人(GentleCP)长期python学习过程中总结的技巧和精华。它不是长篇大论的博客,也不是简单的cheet-sheet ...design :如何编写一个更amazing的python项目,远离脚本小子的

    Python Reinforcement Learning Projects - 2018.pdf

    off point with some amazing Python implementations, this book is a definite must-have for everyone who wants to master this popular branch of AI without drowning in the technical nonsense. Inside ...

Global site tag (gtag.js) - Google Analytics