tgoop.com/djangolearn_ir/1179
Create:
Last Update:
Last Update:
رفتار تابع id توی پایتون ضایع م کرد
توی جلسه دوم آموزش جنگو برای همکاران هنرستان
یه جا خواستم با تابع id یه مفهومی رو توضیح بدم
اینو نوشتم:
m = 10
m1 = 10
print(id(m))
print(id(m1))
خروجی هر دو print یه عدد شد.
دلیلش رو می دونستید؟
لطفا 👎🏻 بزنید اگه نمی دونستی.
ویرایش:
در راستای فهمیدن دلیلش به اینم رسیدم:
https://docs.python.org/3/c-api/long.html?utm_source=chatgpt.com#c.PyLong_FromLong
لینکه میگه:
CPython keeps an array of integer objects for all integers between -5 and 256. When you create an int in that range you actually just get back a reference to the existing object.
اینم ببینید و خواستید تست کنید:
# tuples
t = ('A', 'B')
print(id(t))
t1 = ('A', 'B')
print(id(t1))
# strings
s1 = 'ABC'
s2 = 'ABC'
print(id(s1))
print(id(s2))
به قول کامنت دوستان Immutable هستن.
Python cache the strings and tuple objects and use them to save memory space.
Caching can work only with immutable objects, notice that integer, string, tuples are immutable. So Python implementation can use caching to save memory space and improve performance.
BY جنگولرن
Share with your friend now:
tgoop.com/djangolearn_ir/1179
