Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

سلاسل التوثيق

الدرس 8 من 28 في دورة Clean Code - كتابة كود أفضل باستخدام Python على Coddy.

سلسلة التوثيق (Document String) أو Docstring تظهر غالباً كـ أول جملة في تعريف الوحدة (module)، أو الدالة (function)، أو الفئة (class)، أو الطريقة (method).

قم بالتصريح عن docstring عن طريق إحاطة السلسلة النصية بـ """ أو ''' على سبيل المثال،

def complex(real=0.0, imag=0.0):
	"""
	Form a complex number.
	
		Parameters:
			real (float) -- the real part (default 0.0)
			imag (float) -- the imaginary part (default 0.0)
	"""
    if imag == 0.0 and real == 0.0:
        return complex_zero
    ...

ألقِ نظرة على كيفية توثيق الدالة complex(real, imag)

لاحظ إزاحة (indentation) جسم التوثيق والـ """

بعد استخدام docstring بهذا الشكل، يمكنك استخدام خاصية __doc__ للحصول على التوثيق،

complex.__doc__

سيخرج ما يلي،

Form a complex number.
	
	Parameters:
		real (float) -- the real part (default 0.0)
		imag (float) -- the imaginary part (default 0.0)
challenge icon

التحدي

سهل

لقد تم إعطاؤك دالة تحتوي على تعليق كتلي بدلاً من سلسلة توثيق (docstring).

مهمتك هي استبداله بسلسلة توثيق (docstring) صالحة، تحقق من حالة الاختبار لتحقيق التنسيق الصحيح لسلسلة التوثيق!

جرّب بنفسك

def sum_binary(a, b):
    # Calculate sum of two integers in binary formmated string
    # Parameters:
    # a (int) -- Integer number
    # b (int) -- Another integer number
    # Returns:
    # binary_sum (str) -- The sum of a and b in binary format
    sum = a + b
    binary_sum = bin(sum)
    return str(binary_sum)[1:] # Final formmating

print(sum_binary.__doc__)

جميع دروس Clean Code - كتابة كود أفضل باستخدام Python