The address of an object can be obtained by using the id()
function. The id()
function returns the address of the memory of it’s argument as base-10 integer.
Obtaining the base-10 address using id()
We can use the function hex()
to convert the base-10 number to base-16
1 2 3 4 |
name = "Stacy" print(f'name = {name}') print(f'address of the name in decimal {id(name)}') |
1 2 |
name = Stacy address of the name in decimal 140492014052656 |
Obtaining the base-10 address using hex()
1 2 3 4 5 |
name = "Stacy" print(f'name = {name}') print(f'address of the name in decimal {id(name)}') print(f'address of the name in hex {hex(id(name))}') |
1 2 3 |
name = Stacy address of the name in decimal 140492014052656 address of the name in hex 0x7fc6d8976530 |
Strictly speaking, name
is not “equal” to “Stacy”. Instead, we can say name
is a reference to a String object containing the value Stacy, and it is located at the memory address 0x7fc6d8976530
.