1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonでthisを使う(バッド)プラクティス

Posted at

概要

某OSSで見つけたテクニック。

bs = BookShelf('XX入門', '初めてのYY', '今さら聞けないZZ')
bs.map_(lambda: print(this))
stdout
XX入門
初めてのYY
今さら聞けないZZ

唐突に出現したthis。一体どこから生えてきたのか?

種明かし

コールバック呼び出し前に細工をしていた。

class Book:
    def __init__(self, name):
        self._name = name
        
    def __str__(self):
        return self._name

class BookShelf:
    def __init__(self, *book_names):
        self._books = [*map(Book, book_names)]
    
    def map_(self, callback):
        for book in self._books:
            callback.__globals__['this'] = book
            callback()
        
        del callback.__globals__['this']

分かってみれば大したこと無いが、
インタプリタやIDEの拡張を疑いしばらく悩みこんでしまった。

バッドプラクティスだと思う理由

次のように、引数として渡した方がPythonicだと思うから。

class BookShelf:
    def __init__(self, *book_names):
        self._books = [*map(Book, book_names)]
    
    def map_(self, callback):
        for book in self._books:
            callback(book)

bs = BookShelf('XX入門', '初めてのYY', '今さら聞けないZZ')
bs.map_(lambda this: print(this))

Zen of Python を大事にしたい。

Explicit is better than implicit.
暗示するより明示するほうがいい。

参考Qiita - プログラマが持つべき心構え (The Zen of Python)

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?