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👍202



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: |

Telegram channels fall into two types: Activate up to 20 bots Judge Hui described Ng as inciting others to “commit a massacre” with three posts teaching people to make “toxic chlorine gas bombs,” target police stations, police quarters and the city’s metro stations. This offence was “rather serious,” the court said. Co-founder of NFT renting protocol Rentable World emiliano.eth shared the group Tuesday morning on Twitter, calling out the "degenerate" community, or crypto obsessives that engage in high-risk trading. You can invite up to 200 people from your contacts to join your channel as the next step. Select the users you want to add and click “Invite.” You can skip this step altogether.
from us


Telegram جنگولرن
FROM American