عبارة with بالتفصيل
افهم آلية عمل with وسبب منعها لتسرّب الموارد
عبارة with بالتفصيل درس مجاني في Python Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Python Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Python Academy 4 دروس في المجموع.
مقدمة
الاستخدام الأساسي لـ with
with open('/tmp/cm_test.txt','w') as f:
f.write('hello')
print('file closed:', f.closed)لماذا يوجد with؟
# Without with:
# f = open('f.txt')
# try: ...
# finally: f.close()
# With with:
# with open('f.txt') as f: ...
print('with is cleaner')مديرو سياق متعددون
import tempfile, os
a = tempfile.mktemp(); b = tempfile.mktemp()
with open(a,'w') as fa, open(b,'w') as fb:
fa.write('A'); fb.write('B')
os.unlink(a); os.unlink(b)
print('both closed')لا يخفي with الاستثناءات
try:
with open('/nonexistent.txt') as f:
pass
except FileNotFoundError as e:
print('caught:', e)القفل كمدير سياق
import threading
lock = threading.Lock()
with lock:
print('locked section')
print('lock released')نمط اتصال قاعدة البيانات
# Typical DB pattern:
# with db.transaction() as conn:
# conn.execute(sql)
# commits on success, rolls back on exception
print('db pattern demo')بيانات with متداخلة
import tempfile
tmp = tempfile.mktemp()
with open(tmp,'w') as outer:
with open(tmp,'r+') as inner:
outer.write('x')
import os; os.unlink(tmp)
print('nested exit order')contextlib.suppress
from contextlib import suppress
with suppress(FileNotFoundError):
open('/does/not/exist.txt')
print('no error raised')contextlib.redirect_stdout
from contextlib import redirect_stdout
import io
buf = io.StringIO()
with redirect_stdout(buf):
print('captured')
print('got:', buf.getvalue())ExitStack
from contextlib import ExitStack
import tempfile, os
with ExitStack() as stack:
files = [stack.enter_context(open(tempfile.mktemp(),'w')) for _ in range(3)]
for f in files: f.write('x')
print('all closed')تحقق سريع
مراجعة
واصل التقدم
الأسئلة الشائعة
هل درس «عبارة with بالتفصيل» مجاني؟
نعم — نص درس «عبارة with بالتفصيل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Python Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Python Academy 4 دروس في المجموع.
ماذا ستتعلم في «عبارة with بالتفصيل»؟
افهم آلية عمل with وسبب منعها لتسرّب الموارد تتمرن على Python Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Python Academy؟
لا تُشترط خبرة سابقة. Python Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «عبارة with بالتفصيل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Python Academy هذا؟
نعم. كل درس في Python Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- عبارة with بالتفصيل
- بروتوكول __enter__ و__exit__
- contextlib: @contextmanager
- أنماط مديري السياق العملية