オブジェクトのtype(型)とidentity(識別値)のはなし 100daysofcode(014/100)

学習時間

PyQ3時間 済
読書1時間 予定

学習したこと

  • id関数

組み込み関数のid()はobjectのidentity(識別値)で、整数で表される。この整数はしばしばメモリーにおけるlocation(住所?)に対応している。これはPythonに特有の実装である。このisは2つのオブジェクトのidentityを比べる。組み込み関数のtype()はオブジェクトのタイプを返す。

# Compare Two Objects
def compare(a, b):
print("The identity of a is", id(a))
print('The identity of b is', id(b))

if a is b:
print('a and b are the same object')
if a == b:
print('a and b have the same value')
if type(a) is type(b):
print('a and b have the same type')

a = 'a'
b = 'b'

compare(a,b)

# 実行結果
The identity of a is 10692256
The identity of b is 10717088
a and b have the same type

#Typeは一緒だが、identityは異なる。

引用元: David M. Beazley「Object Identity and Type」http://www.informit.com/articles/article.aspx?p=453682&seqNum=2 (07/02, 2018)

typeについてはこちら*1が勉強になりました。

class Sample1:
a = 100

e = Sample1()
f = Sample1()

if e is f:
print('e and f have the same identity')
if not e is f:
print('e and f have the different identity')

#実行結果
# e and f have the different identity
  • keyword引数と位置引数
    • keyword引数 : 関数のカッコの中に識別子(identifier)がついているもの. ex.)func(a=10, b=120)
    • 位置引数(positional argument):keyword引数以外の引数. ex.) func(3,5)
    • 用語集 — Python 3.6.5 ドキュメント
    • キーワード引数は関数を使う時にkeyword=1のように指定しないといけない.

分からないこと

特になし。

思ったこと(頭に浮かんだこと)

PyQでコード書いている時に、自動で入力補完されないので、これはちゃんと横着せずにキーボードで打ち込みなさいってことなんだなと
思っていたら、Tabキーで入力補完できた...今までの時間...