2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonic って?

2
Last updated at Posted at 2025-12-12

初めに

Pythonを書くようになり、Pythonicがなにかわからなくなったので、まとめ直す。

Pythonicとは

あなたのPythonインタープリタで

import this

と入力すると

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

という文章が出てくる。 
Pythonic はこのThe Zen of Pythonに則ったものをいうらしい。
つまりPythonの言語設計思想に沿った、簡潔で読みやすく、効率的なコードの書き方を指す。単に文法的に正しいだけでなく、Pythonらしい慣習やイディオムをうまく使って、Pythonの機能を最大限に引き出すことを意味する。

具体例

  1. 命名規則を守る
    変数にはスネークケース、クラスにはパスカルケースを使う。

    pythonic_name = 10
    class PythonicClass:
        pass
    
    unPythonicName = 10
    class un_pythonic_class:
        pass
    
  2. 同じ処理は「for + if + append」ではなく 内包表記(list comprehension) を使う
    悪い例:

    result = []
    for x in items:
        if x > 10:
            result.append(x * 2)
    

    Pythonic:

    result = [x * 2 for x in items if x > 10]
    

    こちらの方が読みやすい

  3. 文字列の結合は + より join
    悪い例:

    msg = ""
    for s in strings:
        msg += s
    

    Pythonic:

    msg = "".join(strings)
    
  4. 例外を正しく使う(エラーは無視しない)
    悪い例:

    try:
        risky()
    except:
        pass
    

    Pythonic:

    try:
        risky()
    except ValueError:
        handle()
    
  5. Truthy/Falsyを活用する
    悪い例:

    if len(items) != 0:
        ...
    

    Pythonic:

    if items:
        ...
    
  6. enumerate()zip()を上手に使う
    悪い例:

    i = 0
    for item in items:
        print(i, item)
        i += 1
    

    Pythonic:

    for i, item in enumerate(items):
        print(i, item)
    
    for a, b in zip(list1, list2):
    ...
    

    手動でインデックスを回すな

  7. with を使って安全にリソース管理
    悪い例:

    f = open("a.txt")
    data = f.read()
    f.close()
    

    Pythonic:

    with open("a.txt") as f:
        data = f.read()
    
  8. EAFP(やってみてダメなら例外処理)を採用
    例:

    try:
    value = d[key]
    except KeyError:
        value = default
    

終わりに

Elephant
~Thank you for reading~

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?