LoginSignup
1
2

More than 5 years have passed since last update.

Python > id() / global を試してみた > id()が返すID / 代入先の辞書 {ローカル辞書 / グローバル辞書} | locals(), globals()

Last updated at Posted at 2017-04-13

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 2852 / 12833)

id()globalの例がある。

参考: id(object)
参考: global

試してみた。

animal = 'dog'

def my_func_local():
    animal = 'cat'
    print(id(animal))

def my_func_global():
    global animal
    animal = 'wyvern'
    print(id(animal))

print(id(animal))
my_func_local()
print(animal)
my_func_global()
print(animal)
run
47889501431096
47889502846848
dog
47889501431208
wyvern

dogとwyvernは同じidかと思うと違った。
stringsがimmutableであること関係あるのか未消化。

教えていただいた事項

@yubaさんのコメントにてid()が返すIDについて教えていただきました。

情報感謝です。

@shiracamus さんのコメントにて、代入先の辞書(ローカル辞書、グローバル辞書)について教えていただきました。

情報感謝です。

1
2
18

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