3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

僕が好きじゃないPythonの仕様

Posted at

TL;DR;

>>> ls_a = ['a' 'A']
>>> ls_a
['aA']

はじめに

みなさん、エラーについてどう思ってますか?筆者はエラーが出ると萎えますが、間違ってるところを指摘してくれるし、どうすれば解決する(maybe)かも書いてくれるので、必ずしも嫌いではありません。

>>> ls_a = ['a', 'A]
  File "<python-input-10>", line 1
    ls_a = ['a', 'A]
                 ^
SyntaxError: unterminated string literal (detected at line 1)

上記の例だと、文字列を ' で閉じ忘れてるって言ってくれてますね。
さて、筆者は競プロ・研究etc で Python を使うんですよ。そんな中、全然想定と違う動きをすることがありました。

本題

  • 2つの str を配列に入れて初期化しました。
  • その配列の要素を確認してみましょう。
>>> ls_a = ['a' 'A']
>>> ls_a
['aA']

???
え。なんで繋がってるんだ? str って勝手につなげるの?(エラー吐いてくれないの?)なので、以下のコードを試してみました。

>>> str_b = 'b' 'B'
>>> str_b
'bB'

どうやら、文字列の連結は + を用いなくても実現できるようです。一見便利な仕様かもしれませんが、配列を初期化するときにカンマを忘れるとバグにつながるかもしれませんね。。。

おわりに

  • まあ、linter formatter を入れればこういうことも減らせるかもしれないんですけどね...
3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?