タグ

関連タグで絞り込む (1)

タグの絞り込みを解除

Pythonとdecoratorに関するoinumeのブックマーク (7)

  • デコレータを書く時にはfunctools.wrapsを使おう - スコトプリゴニエフスク通信

    トリビアルな例だが、元の関数(func)をデコレート・ラップするmydecoratorというデコレータを書いた時、下のように単純にinnerを返すと、 def mydecorator(func): def inner(*args, **kwds): print "Hi, I'm inner!" return func(*args, **kwds) return inner @mydecorator def hello(to): """ Say hello to somebody """ print "Hello, %s!" % to if __name__ == '__main__': print repr(hello) print hello.__doc__ 結果として、 None のように、デコレートされた元の関数(hello)の関数名やドキュメント文字列が失われてしまう。デコレータを

  • Introspection to get decorator names on a method?

    I am trying to figure out how to get the names of all decorators on a method. I can already get the method name and docstring, but cannot figure out how to get a list of decorators.

    Introspection to get decorator names on a method?
  • PythonDecoratorLibrary - Python Wiki

    This page is meant to be a central repository of decorator code pieces, whether useful or not <wink>. It is NOT a page to discuss decorator syntax! Feel free to add your suggestions. Please make sure example code conforms with PEP 8. Creating Well-Behaved Decorators / "Decorator decorator" Note: This is only one recipe. Others include inheritance from a standard decorator (link?), the functools @w

  • Python入門:デコレータとは

    前から常々思っていることだが、何かについて勉強する一番効率的な方法はそれを誰かに教えること。人に教えようとすると、それなりに準備をしなければならないし、自分の頭の中を整理しなければならない。また教える過程でするどい質問をされたり間違いを指摘されて、さらに勉強を強いられることもある。 私がこの手の「入門編エントリー」を書くのは、ほとんどの場合「自分自身の理解をより深めたい」ことが一番の目的であるが、ブログの場合、教室などと違って「その道の達人」みたいな人たちがツッコミを入れてくれるケースもしばしばあるので、そのメリットは何倍にもなる。 先日のクロージャに関するエントリーなどは良い例で、「そんな用途にはmemoizeというデコレータが便利」などの指摘がいただけだけであれを書いた価値があるというもの。 そこで、今日はPythonのデコレータに関して。デコレータがPythonという言語に導入された

  • PythonのWebフレームワーク使うなら知っておきたいデコレータ - 神様なんて信じない僕らのために

    最近「オワタ\(^o^)/」で有名なDjangoしか触ってないダメ人間です。 こんにちは。 Djangoとかどうでもいいがな、 Webフレームワークとかめんどくさいがな、 という最近なのでDつながりでDecoratorの話をします。 ナウでヤングなPythonistaのホットな話題はGCの参照カウンタ、 ではなくてFlaskとかかもしれないですが、 @app.route("/") def hello(): return "Hello World!" こいつも多分に漏れずDecoratorを使います。 Djangoでも、 @require_GET とか @require_POST とか使ったり見たことがあるんじゃないかと思います。 で、意外と魔法っぽいデコレータですが、 これっていったいどうなってんの? って事を知らない人が割といたりします。 「とりあえず指定しろって言われたから指定してます

    PythonのWebフレームワーク使うなら知っておきたいデコレータ - 神様なんて信じない僕らのために
  • [メモ] Pythonのデコレータ – taichino.com

    出てくる度に調べる割に、いつまで経っても覚えられなかったデコレータですが、今回こそ仕留める為にメモエントリーです。まずよく見かけるのは関数の時間を計測するという例ですね。僕はこの形だけを何となく覚えては忘れていました。 #!/usr/bin/python # -*- coding: utf-8 -*- import time # デコレータ定義 def time_func(func): def decorator(*args): start = time.time() ret = func() print '%s was executed, it took %s sec' % (func.func_name, time.time() - start) return ret return decorator # デコレータ適用 @time_func def test(): time.slee

  • PEP 318 – Decorators for Functions and Methods | peps.python.org

    PEP 318 – Decorators for Functions and Methods Author: Kevin D. Smith <Kevin.Smith at theMorgue.org>, Jim J. Jewett, Skip Montanaro, Anthony Baxter Status: Final Type: Standards Track Created: 05-Jun-2003 Python-Version: 2.4 Post-History: 09-Jun-2003, 10-Jun-2003, 27-Feb-2004, 23-Mar-2004, 30-Aug-2004, 02-Sep-2004 Table of Contents WarningWarningWarning Abstract Motivation Why Is This So Hard? Bac

    PEP 318 – Decorators for Functions and Methods | peps.python.org
  • 1