LoginSignup
2
1

More than 3 years have passed since last update.

Pythonの定数、Noneとか(リファレンスによると)

Last updated at Posted at 2020-08-09

はじめに

pythonの定数について調べた。
しっくりは、きていませんが。

pythonの定数

python, 定数
とかでぐぐると、リファレンスの以下の項にたどり着く。
https://docs.python.org/ja/3/library/constants.html

以下、引用。

組み込み定数
組み込み名前空間にはいくつかの定数があります。定数の一覧:

念のため、
https://docs.python.org/3/library/constants.html
も引用しておく

Built-in Constants
A small number of constants live in the built-in namespace. They are:

ここでは、以下の6つが示されている。

False
True
None
NotImplemented
Ellipsis
__debug__

値をみてみる

>>> False
False
>>>
>>> True
True
>>>
>>> None
>>>
>>> NotImplemented
NotImplemented
>>>
>>> Ellipsis
Ellipsis
>>>
>>> __debug__
True
>>>

値を代入してみる

>>>
>>> False = 3
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>>
>>> True = 3
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>>
>>> None = 3
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>>
>>> NotImplemented = 3
>>>
>>> Ellipsis = 3
>>>
>>> __debug__ = 3
  File "<stdin>", line 1
SyntaxError: assignment to keyword
>>>
>>>

⇒ ↓↓↓NotImplementedとEllipsisは、値が代入できてしまう。(定数なのに。。)ちょっと、意味の違う定数なんでしょう。

>>> NotImplemented
3
>>>
>>> Ellipsis
3
>>>
>>> type(NotImplemented)
<class 'int'>
>>> type(Ellipsis)
<class 'int'>
>>>

↑↑↑ 本気で、代入されていますね。。。intになっていますね。。。

補足

コメント頂いたので補足。
False
True
None
は、キーワードでしょう、とのこと。

以下の
https://docs.python.org/ja/3/reference/lexical_analysis.html#identifiers
を引用すると、

2.3.1. キーワード (keyword)¶
以下の識別子は、予約語、または Python 言語における キーワード (keyword) として使われ、通常の識別子として使うことはできません。キーワードは厳密に下記の通りに綴らなければなりません:

False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

また、
コメントで示して頂いた
http://python-history.blogspot.com/2013/11/story-of-none-true-false.html
https://www.python.org/dev/peps/pep-0285/
も参考になります。

まとめ

あまりしっくりは、きませんでした。
コメントなどあればお願いします。:candy:

どちらかというと、『リファレンスで残念な気分になったあるある』という感じの記事にいまのところなっています。。。

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