I tried Python> id () / global> ID returned by id () / dictionary to assign to {local dictionary / global dictionary} | locals (), globals ()

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

There are examples of ʻid () and global`.

Reference: id (object) Reference: global

I've tried.

http://ideone.com/7ZBN92

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

I thought dog and wyvern had the same id, but they were different. Undigested whether it is related to the fact that strings are immutable.

Matters taught

@ yuba's Comment told me about the ID returned by ʻid () `.

Thank you for the information.

@ shiracamus's Comment told me about the dictionary to assign to (local dictionary, global dictionary).

Thank you for the information.

Recommended Posts

I tried Python> id () / global> ID returned by id () / dictionary to assign to {local dictionary / global dictionary} | locals (), globals ()
Python3> Functions> Symbol table> Assign to variables / Reference variables / Global variables / globals () / locals ()
I tried to touch Python (installation)
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to communicate with a remote server by Socket communication with Python.
Python -I tried to restore the dictionary comprehensive notation to its original form-
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to implement ADALINE in Python
python: How to use locals () and globals ()
I tried to implement PPO in Python
I tried to verify and analyze the acceleration of Python by Cython
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
I tried to analyze the New Year's card by myself using python
[Introduction to Docker] I tried to summarize various Docker knowledge obtained by studying (Windows / Python)
I tried to program bubble sort by language
I tried to get an image by scraping
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to implement TOPIC MODEL in Python
I tried using the Datetime module by Python
I tried to automate sushi making with python
I want to sell Mercari by scraping python
I tried to implement selection sort in python
I tried to classify dragon ball by adaline
I tried to extract various information of remote PC from Python by WMI Library
I tried to aggregate & compare unit price data by language with Real Gachi by Python
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
I tried to graph the packages installed in Python
I tried to summarize how to use matplotlib of python
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
Continuation ・ I tried to make Slackbot after studying Python3
I tried to get started with blender python script_Part 02
I tried to implement Dragon Quest poker in Python
I tried to implement an artificial perceptron with python
I tried to implement GA (genetic algorithm) in Python
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to automatically generate a password with Python3
I tried to summarize how to use pandas in python
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
[Python] I tried to get Json of squid ring 2
I tried to access Google Spread Sheets using Python
I tried to summarize the string operations of Python
I tried to solve AOJ's number theory with Python
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
I tried to debug.
I tried to paste
I tried Python> decorator
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to create API list.csv in Python from swagger.yaml
I tried various methods to send Japanese mail with Python