TECH EXPERT 43日目

TECH::EXPERT

新しいチームでの開発へ移る。個人アプリの開発が滞りそうだ。
メンバーには恵まれていると思うのでそれほど苦戦なくカリキュラムは終わりそうな予感がする。

python

None

nilでもnillでもnullでもなく、None.
しかも先頭は大文字。
JSでいうundefinedもnullもNoneで一括り。

if object と書かずに、
if object is not Noneと書きましょう。

Comparisons to singletons like None should always be done with is
 or is not, never the equality operators.
Also, beware of writing if x
 when you really mean if x is not None
 — e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!

PEP 8 – Style Guide for Python Code | peps.python.org
PythonEnhancementProposals(PEPs)

正規表現オブジェクト

re.compile()で正規表現オブジェクトへ変換できる。

文字列の前にrをつけるとエスケープシーケンスを使わずにかける。
以下の例ではrはあってもなくても変わらないが、\\と打たなくて済むので楽。

import re

pattern = r"四半期"
repattern = re.compile(pattern)

match()

文頭から文字列一致を検索。
全文検索はsearchを使う。

import re

m = re.match(pattern, string):
print(m)
=> <re.Match object; span=(2, 5), match='四半期'>
group()正規表現にマッチした文字列を返す。
start()マッチの開始位置を返す。
end()マッチの終了位置を返す。
span()マッチの位置 (start, end) を含むタプルを返す。

continue / pass

passは何もしない。処理を止めもしないし、なにか処理をするわけでもない。
一方、ifやfor文で使われるcontinueは以降の処理を行わず、次のステップに移るという動作になる。なのでcontinue以下に処理を書いても読まれない。

式展開 .format()

{}を.format()の引数で置き換える。

hoge = 55
fuga = 22
"hogehoge{}hoge{}hoge".format(fuga,hoge)
=> hogehoge22hoge55hoge

コメント