gbt1988 / PythonToWork

0 stars 0 forks source link

关于内存地址,ID, frame,object #16

Open gbt1988 opened 4 years ago

gbt1988 commented 4 years ago

https://medium.com/@daniel.tooke/variables-and-memory-addresses-in-python-6d96d672ed3d

gbt1988 commented 4 years ago

https://groups.google.com/forum/#!topic/comp.lang.python/LdQIdn1DGa4 python 不能通过memory 地址访问

gbt1988 commented 4 years ago

https://www.edureka.co/community/53766/how-to-distinguish-between-a-variable-and-an-identifier 变量和指针的区别

gbt1988 commented 4 years ago

https://www.quora.com/How-can-we-print-a-memory-address-occupied-by-a-variables-value-in-Python-programming 打印变量值对应的内存地址print(hex(id(x)))

gbt1988 commented 4 years ago

https://realpython.com/python-memory-management/ 内存管理 python c 底层

gbt1988 commented 4 years ago

https://realpython.com/pointers-in-python/ id 和 memory 是不同的,但是这里面介绍id() returns the object’s memory address.可能是有别的解释吧。下面的代码显示loader id 和 hex 打印出的内存地址是不同的

/Users/kevin/pycc/venv/bin/python /Users/kevin/pycc/car.py
2016 Tesla Model S
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10f887898>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/kevin/pycc/car.py', '__cached__': None, 'sys': <module 'sys' (built-in)>, 'Car': <class '__main__.Car'>, 'ElectricCar': <class '__main__.ElectricCar'>, 'my_tesla': <__main__.ElectricCar object at 0x10f8e8320>}
__name 的 id 地址是4555532336
Car的 id 地址是140440721509960
x的 id 地址是4553186480
y的 id 地址是4553186480
x的 内存物理 地址是0x10f641cb0
__loader__的 id 地址是4555569304
__loader__的 内存物理 地址是0x10f887898
sys返回的__loader的reference count是2
sys返回的Car的reference count是8

Process finished with exit code 0
import sys
class Car():
    def __init__(self,make,model,year):
        self.make = make
        self.model = model
        self.year =year
        self.odometer_reading = 0
    def get_descriptive_name(self):
        long_name = str(self.year) + ' ' +self.make + ' ' + self.model
        return long_name.title()
class ElectricCar(Car):
    def __init__(self,make,model,year):
        super().__init__(make,model,year)

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
print(locals())
print('__name 的 id 地址是' + str(id(__name__)))
print('Car的 id 地址是' + str(id(Car)))

x = 4
y = 4

print('x的 id 地址是' + str(id(x)))
print('y的 id 地址是' + str(id(y)))

print('x的 内存物理 地址是' + str(hex(id(x))))
print('__loader__的 id 地址是' + str(id(__loader__)))
print('__loader__的 内存物理 地址是' + str(hex(id(__loader__))))
print('sys返回的__loader的reference count是' + str(sys.getrefcount(__loader__)))
print('sys返回的Car的reference count是' + str(sys.getrefcount(Car)))
gbt1988 commented 4 years ago

https://blog.csdn.net/heipark_/article/details/48369467 python内置id()函数,这个函数用于返回对象的唯一标识(identity)。对象实际内存地址为hex(id(obj)),本文我将id()和内存地址划等号。

gbt1988 commented 4 years ago

id() 函数返回对象的唯一标识符,标识符是一个整数。

CPython 中 id() 函数用于获取对象的内存地址。 https://www.runoob.com/python/python-func-id.html