Telegram Group & Telegram Channel
Forwarded from Python BackendHub
داشتم ویدیو مصاحبه عثمان رو میدیدم با امیربهادر که شاخکام تکون خورد بیشتر راجب yield تحقیق کنم.

سوال) کاربرد yield چیه تو پایتون؟
جواب: یک جنریتور فانکشن که memory efficient هست و مثلا از دیسک میخواین ۱۰۰ گیگ فایل بخونید نیاز نیست کلشو یک دفعه لود کنید تو مموری و میتونید ۱۰۰ مگ ۱۰۰ مگ لود کنید تو مموری

مقاله اول) yield چطور تو سطح پایتون کار میکنه؟ (نسبتا ساده, اسکیپ کنید اگه بلدین)
همونطور که گفتم yield یک جنریتور فانکشن هست. ولی یعنی چی؟
یعنی وقتی کلمه yield رو میذارین و اون تابع رو ران میکنید به جای اینکه یک آبجکت بهتون ریترن کنه بهتون یک جنریتور ریترن میکنه.

هردفعه که next() رو بزنید یک بار اجرا میشه و تا yield بعدی میره.اگه yield دیگه ای نباشه دیگه چیزی ران نمیکنه و ارور StopIteration میده.
یک نکته کلیدی. پس وقتی میخوایم کلین آپ کنیم همینطوری next(my_generator) رو صدا میزنیم تا به اخرین خط کد برسه و ارور بده. درواقع تو asyncio pytest همین اتفاق میفته. ما یک آبجکت رو yield میکنیم. خود asyncio-pytest پلاگین میاد next() میزنه روش و آبجکتو میگیره. بعد به کل تستا پاس میده همون آبجکتو. بعدش هم یک دور دیگه next() رو میزنه و میدونه ما بعد yield اول کلین آپ کردیم. اون موقع کلین آپ تست ما هم ران میشه که خیلی قشنگ تر unittest پایتون هست.



def my_gen():
yield 1
print("Clean up here")

generator = my_gen()
next(generator) -> 1
next(generator) -> prints clean up here -> raise StopIteration


خب تا اینجا فهمیدیم تو سطح پایتون چطوری کار میکنه generator و yield.
حالا میریم یک لایه پایین تر تو سطح interpreter 😈

@ManiFoldsPython



tg-me.com/djangolearn_ir/1047
Create:
Last Update:

داشتم ویدیو مصاحبه عثمان رو میدیدم با امیربهادر که شاخکام تکون خورد بیشتر راجب yield تحقیق کنم.

سوال) کاربرد yield چیه تو پایتون؟
جواب: یک جنریتور فانکشن که memory efficient هست و مثلا از دیسک میخواین ۱۰۰ گیگ فایل بخونید نیاز نیست کلشو یک دفعه لود کنید تو مموری و میتونید ۱۰۰ مگ ۱۰۰ مگ لود کنید تو مموری

مقاله اول) yield چطور تو سطح پایتون کار میکنه؟ (نسبتا ساده, اسکیپ کنید اگه بلدین)
همونطور که گفتم yield یک جنریتور فانکشن هست. ولی یعنی چی؟
یعنی وقتی کلمه yield رو میذارین و اون تابع رو ران میکنید به جای اینکه یک آبجکت بهتون ریترن کنه بهتون یک جنریتور ریترن میکنه.

هردفعه که next() رو بزنید یک بار اجرا میشه و تا yield بعدی میره.اگه yield دیگه ای نباشه دیگه چیزی ران نمیکنه و ارور StopIteration میده.
یک نکته کلیدی. پس وقتی میخوایم کلین آپ کنیم همینطوری next(my_generator) رو صدا میزنیم تا به اخرین خط کد برسه و ارور بده. درواقع تو asyncio pytest همین اتفاق میفته. ما یک آبجکت رو yield میکنیم. خود asyncio-pytest پلاگین میاد next() میزنه روش و آبجکتو میگیره. بعد به کل تستا پاس میده همون آبجکتو. بعدش هم یک دور دیگه next() رو میزنه و میدونه ما بعد yield اول کلین آپ کردیم. اون موقع کلین آپ تست ما هم ران میشه که خیلی قشنگ تر unittest پایتون هست.



def my_gen():
yield 1
print("Clean up here")

generator = my_gen()
next(generator) -> 1
next(generator) -> prints clean up here -> raise StopIteration


خب تا اینجا فهمیدیم تو سطح پایتون چطوری کار میکنه generator و yield.
حالا میریم یک لایه پایین تر تو سطح interpreter 😈

@ManiFoldsPython

BY جنگولرن


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/djangolearn_ir/1047

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

That strategy is the acquisition of a value-priced company by a growth company. Using the growth company's higher-priced stock for the acquisition can produce outsized revenue and earnings growth. Even better is the use of cash, particularly in a growth period when financial aggressiveness is accepted and even positively viewed.he key public rationale behind this strategy is synergy - the 1+1=3 view. In many cases, synergy does occur and is valuable. However, in other cases, particularly as the strategy gains popularity, it doesn't. Joining two different organizations, workforces and cultures is a challenge. Simply putting two separate organizations together necessarily creates disruptions and conflicts that can undermine both operations.

Telegram auto-delete message, expiring invites, and more

elegram is updating its messaging app with options for auto-deleting messages, expiring invite links, and new unlimited groups, the company shared in a blog post. Much like Signal, Telegram received a burst of new users in the confusion over WhatsApp’s privacy policy and now the company is adopting features that were already part of its competitors’ apps, features which offer more security and privacy. Auto-deleting messages were already possible in Telegram’s encrypted Secret Chats, but this new update for iOS and Android adds the option to make messages disappear in any kind of chat. Auto-delete can be enabled inside of chats, and set to delete either 24 hours or seven days after messages are sent. Auto-delete won’t remove every message though; if a message was sent before the feature was turned on, it’ll stick around. Telegram’s competitors have had similar features: WhatsApp introduced a feature in 2020 and Signal has had disappearing messages since at least 2016.

telegram from ru


Telegram جنگولرن
FROM USA