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

The lead from Wall Street offers little clarity as the major averages opened lower on Friday and then bounced back and forth across the unchanged line, finally finishing mixed and little changed.The Dow added 33.18 points or 0.10 percent to finish at 34,798.00, while the NASDAQ eased 4.54 points or 0.03 percent to close at 15,047.70 and the S&P 500 rose 6.50 points or 0.15 percent to end at 4,455.48. For the week, the Dow rose 0.6 percent, the NASDAQ added 0.1 percent and the S&P gained 0.5 percent.The lackluster performance on Wall Street came on uncertainty about the outlook for the markets following recent volatility.

Telegram and Signal Havens for Right-Wing Extremists

Since the violent storming of Capitol Hill and subsequent ban of former U.S. President Donald Trump from Facebook and Twitter, the removal of Parler from Amazon’s servers, and the de-platforming of incendiary right-wing content, messaging services Telegram and Signal have seen a deluge of new users. In January alone, Telegram reported 90 million new accounts. Its founder, Pavel Durov, described this as “the largest digital migration in human history.” Signal reportedly doubled its user base to 40 million people and became the most downloaded app in 70 countries. The two services rely on encryption to protect the privacy of user communication, which has made them popular with protesters seeking to conceal their identities against repressive governments in places like Belarus, Hong Kong, and Iran. But the same encryption technology has also made them a favored communication tool for criminals and terrorist groups, including al Qaeda and the Islamic State.

telegram from ms


Telegram جنگولرن
FROM USA