تنسيق الشِفرة

2.7 تنسيق الشِفرة

أهداف التعلم

في نهاية هذا القسم، ينبغي أن تكون قادرًا على:

  • تحديد التباعد المناسب للتعبيرات والتعليمات.
  • كتابة تعليمات متعددة الأسطر باستخدام الربط الضمني للأسطر.

التباعد الموصى به

تُتجاهَل معظم المسافات في شِفرة Python عند تشغيل البرامج؛ ومع ذلك، فإن المسافات في بداية السطر مهمة جدًا. البرنامجان التاليان متكافئان:

  • تباعد جيد: name = input("Enter someone's name: ") place = input("Enter a famous place: ") print(name, "should visit", place + "!")
  • تباعد سيء: name=input ("Enter someone's name: " ) place =input("Enter a famous place: ") print( name,"should visit" , place+ "!")

قد يجادل البعض بأن المسافات المفقودة أو الزائدة لا تهم. ففي النهاية، يعمل البرنامجان أعلاه بالطريقة نفسها تمامًا. ومع ذلك، فإن نسخة "التباعد السيء" أصعب في القراءة. الشِفرة مثل name=input و place+ قد تؤدي إلى الارتباك.

يكتب المبرمجون الجيدون شِفرة سهلة القراءة قدر الإمكان. وبهذه الطريقة، يكون المبرمجون الآخرون أكثر قدرة على فهم الشِفرة. لتشجيع الاتساق، لدى مجتمع Python مجموعة من الإرشادات حول مكان وضع المسافات والأسطر الفارغة، وكيفية تسمية المُتغيِّرات، وكيفية تقسيم الأسطر الطويلة، ومواضيع أخرى مهمة.

دليل أسلوب بايثون

PEP 8 هو دليل الأسلوب الرسمي لـ Python. PEP هو اختصار لـ Python Enhancement Proposal. يكتب أعضاء مجتمع Python مستنداتِ PEP لتوثيق أفضل الممارسات واقتراح ميزات جديدة. يستند الجدول أدناه إلى إرشادات من PEP 8 تحت عنوان المسافات البيضاء (Whitespace) في التعبيرات والتعليمات.

إرشادات مثال أخطاء شائعة
الأقواس: لا مسافة قبلها أو بعدها.

print("Go team!")

print ("Go team!")
print( "Go team!" )

الفواصل: لا مسافة قبلها، مسافة واحدة بعدها.

print("Hello", name)

print("Hello" , name)
print("Hello",name)

الإسناد: مسافة واحدة قبل وبعد علامة =.

name = input("Your name? ")

name=input("Your name? ")
name= input("Your name? ")
name =input("Your name? ")

الدمج: مسافة واحدة قبل وبعد علامة +.

print("Hi", name + "!")

print("Hi", name+"!")
print("Hi", name+ "!")
print("Hi", name +"!")

العمليات الحسابية: استخدم المسافات لإظهار أسبقية أقل.

x**2 + 5*x - 8

x ** 2 + 5 * x - 8
x ** 2+5 * x-8
x**2+5*x-8

جدول 2.7 إرشادات للمسافات.

مفاهيم تطبيقية

التباعد الموصى به

1.
أي تعليمة منسقة بشكل صحيح؟
  1. name = input("What is your name? ")
  2. name = input ("What is your name? ")
  3. name = input( "What is your name? " )
2.
أي تعليمة منسقة بشكل صحيح؟
  1. name=name+"!"
  2. name = name+"!"
  3. name = name + "!"
3.
أي تعليمة منسقة بشكل صحيح؟
  1. print("Hello",name)
  2. print("Hello", name)
  3. print("Hello " , name)
4.
أي تعبير منسق بشكل صحيح؟
  1. b**2 - 4*a*c
  2. b ** 2 - 4 * a * c
  3. b**2 - 4*a * c

الدمج التلقائي

السلاسل النصية الطويلة تجعل برامج Python صعبة القراءة. مثال: يطبع هذا البرنامج الجملة الأولى من إعلان الاستقلال الأمريكي:

print("The unanimous Declaration of the thirteen united States of America, When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.")

يوصي PEP 8 بأن يكون طول كل سطر من الشِفرة أقل من 80 حرفًا. وبهذه الطريقة، لن يحتاج المبرمجون إلى التمرير أفقيًا لقراءة الشِفرة. يمكن إعادة كتابة البرنامج أعلاه عن طريق تقسيم السلسلة النصية الأصلية:

     print("The unanimous Declaration of the thirteen united States of "
           "America, When in the Course of human events, it becomes "
           "necessary for one people to dissolve the political bands "
           "which have connected them with another, and to assume among "
           "the powers of the earth, the separate and equal station to "
           "which the Laws of Nature and of Nature's God entitle them, a "
           "decent respect to the opinions of mankind requires that they "
           "should declare the causes which impel them to the separation.")

للتسهيل، تدمج Python سلاسلَ نصيةً متعددةً تلقائيًا. لا يلزم استخدام عامل + في هذه الحالة.

نقطة تحقق

دمج السلاسل النصية

مفاهيم تطبيقية

دمج السلاسل النصية الحرفية

5.
أي سطر يطبع كلمة "grandmother"؟
  1. print(grandmother)
  2. print("grand" "mother")
  3. print("grand", "mother")
6.
ما السلسلة النصية المكافئة لـ "Today is" "a holiday"؟
  1. 'Today isa holiday'
  2. 'Today is a holiday'
  3. 'Today is" "a holiday'
7.
إذا كانت name هي "Ada"، فماذا يخرج print("Hello," name)؟
  1. Hello,Ada
  2. Hello, Ada
  3. SyntaxError

تعليمات متعددة الأسطر

تحتاج معظم التعليمات في برنامج Python إلى سطر واحد فقط من الشِفرة. ولكن في بعض الأحيان، تحتاج التعليمات الأطول إلى الامتداد عبر أسطر متعددة. توفر Python طريقتين لكتابة تعليمات متعددة الأسطر:

  • الربط الصريح للأسطر، باستخدام أحرف \:

    decl = "The unanimous Declaration of the thirteen united States of " \         "America, When in the Course of human events, it becomes " \         "necessary for one people to dissolve the political bands..."
  • الربط الضمني للأسطر، باستخدام الأقواس:

    decl = ("The unanimous Declaration of the thirteen united States of "         "America, When in the Course of human events, it becomes "         "necessary for one people to dissolve the political bands...")

الربط الضمني للأسطر أكثر شيوعًا، حيث تستخدم العديد من التعليمات والتعبيرات الأقواس على أي حال. يوصي PEP 8 بتجنب استخدام الربط الصريح للأسطر كلما أمكن ذلك.

مفاهيم تطبيقية

تعليمات متعددة الأسطر

8.
أي حرف يستخدم للربط الصريح للأسطر؟
  1. /
  2. \
  3. |
9.
ما أفضل طريقة لطباعة سلسلة نصية طويلة جدًا؟
  1. تقسيم السلسلة النصية إلى عدة سلاسل نصية أصغر.
    print("..." # الجزء الأول من السلسلة النصية
    "..." # الجزء التالي من السلسلة النصية
    "...")
  2. طباعة السلسلة النصية باستخدام تعليمات print متعددة.
    print("...") # الجزء الأول من السلسلة النصية
    print("...") # الجزء التالي من السلسلة النصية
    print("...")
  3. إسناد السلسلة النصية إلى مُتغيِّر وطباعة المُتغيِّر.
    text = "..." # السلسلة النصية الكاملة
    print(text)
    
10.
أي مثال يتكون من تعليمتين؟
  1. print("Happy "
    "New Year")
  2. saying = ("Happy "
    "New Year")
  3. saying= "Happy "
    "New Year"

جرّب بنفسك

متباعد

الشِفرة التالية تعمل بشكل صحيح ولكنها منسقة بشكل سيء. على وجه الخصوص، لا تتضمن الشِفرة المسافات الموصى بها من قبل PEP 8. علاوة على ذلك، يبلغ طول سطرين حوالي 90 حرفًا. أعد تنسيق الشِفرة لتتبع الإرشادات في هذا القسم. احرص على عدم تغيير سلوك الشِفرة نفسها.

جرّب بنفسك

خمسة اقتباسات

اكتب برنامجًا يطبع الاقتباسات الخمسة التالية (المصدر: BrainyQuote) من غيدو فان روسوم، مبتكر Python. يجب أن يحتوي برنامجك على خمس تعليمات print بالضبط، واحدة لكل اقتباس:

    1. "If you're talking about Java in particular, Python is about the best fit you can get amongst all the other languages. Yet the funny thing is, from a language point of view, JavaScript has a lot in common with Python, but it is sort of a restricted subset."
    2. "The second stream of material that is going to come out of this project is a programming environment and a set of programming tools where we really want to focus again on the needs of the newbie. This environment is going to have to be extremely user-friendly."
    3. "I have this hope that there is a better way. Higher-level tools that actually let you see the structure of the software more clearly will be of tremendous value."
    4. "Now, it's my belief that Python is a lot easier than to teach to students programming and teach them C or C++ or Java at the same time because all the details of the languages are so much harder. Other scripting languages really don't work very well there either."
    5. "I would guess that the decision to create a small special purpose language or use an existing general purpose language is one of the toughest decisions that anyone facing the need for a new language must make."

لاحظ أن جميع هذه الأسطر أطول من 80 حرفًا، وبعضها يحتوي على علامات اقتباس مفردة. نسّق الشِفرة باستخدام تعليمات متعددة الأسطر وتسلسلات الهروب حسب الضرورة.