`
cloudhe
  • 浏览: 108719 次
  • 性别: 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-...

    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实现去除图片中指定颜色的像素功能示例

    ### Python 实现去除图片中指定颜色的像素功能详解 在图像处理领域,经常需要去除或替换图像中的某些特定颜色,以此来达到预期的效果或者为后续处理做准备。本篇文章将详细解析如何使用Python来实现去除图片中指定...

    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.

    ha_amazing.rar

    很抱歉,根据您提供的信息,"ha_amazing.rar"似乎是一个压缩文件的名称,而描述和标签也重复了相同的文件名,这并没有提供具体...例如,如果"amazing"是一个Python项目,我们可以讨论版本控制、编程规范、测试策略等。

    flit:简化的Python模块包装

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

    amazing-dollop:phonevalueAI网站

    "amazing-dollop"似乎是一个项目的名字,而"phonevalueAI"可能是一个专注于手机价值评估或与手机相关的AI技术的网站。这个项目的重点可能是利用HTML来构建网页。接下来,我将详细介绍HTML以及它在创建网页中的作用,...

    Amazing_maze:COSC 302的最终项目

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

    Dive in to Python

    这本书以"amazing stuffs"为亮点,揭示了Python编程的无限魅力和强大功能。 首先,我们要理解Python的基础。Python是一种高级、通用的解释型编程语言,以其清晰的语法结构和代码可读性而闻名。它的设计哲学强调代码...

    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 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 ...

    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 ...

    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进阶:切片的误区与高级用法》中,我介绍了切片的基础用法、高级用法以及一些使用误区。这些内容都是基于原生的序列类型(如字符串...

Global site tag (gtag.js) - Google Analytics