DJANGOLEARN_IR Telegram 1179
رفتار تابع 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.
👎27👍192



tgoop.com/djangolearn_ir/1179
Create:
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

View MORE
Open in Telegram


Telegram News

Date: |

SUCK Channel Telegram Administrators Write your hashtags in the language of your target audience. Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp. 2How to set up a Telegram channel? (A step-by-step tutorial)
from us


Telegram جنگولرن
FROM American