الافتراضي __init__ لكائنات التعليمات البرمجية
-
02-01-2020 - |
سؤال
لقد وجدت كائنات التعليمات البرمجية في بايثون.أشعر بالفضول لمعرفة ما يفعله كل من المتغيرات في المُنشئين.لا يوجد الكثير من المعلومات عنها في وظيفة المساعدة المضمنة، كل ما حصلت عليه هو:
class code(object)
| code(argcount, nlocals, stacksize, flags, codestring, constants, names,
| varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])
|
| Create a code object. Not for the faint of heart.
ومن الواضح أن هذا ليس مفيدا للغاية.ما هي الأنواع التي تتوقعها كل من هذه المدخلات، وماذا تفعل القيم؟ ملحوظة: لقد طرحت هذا السؤال بدافع الفضول الأكاديمي، وليس لأي غرض برمجي محدد.
المحلول
كائن كود Python هو في الغالب مجرد حاوية لسماته.تصبح كل من الوسيطات التي تراها للمنشئ سمة ذات co_
البادئة (على سبيل المثالال argcount
تصبح الحجة co_argcount
يصف).
يقوم المُنشئ ببعض التحقق من الصحة، لذلك إذا لم تكن الوسيطات من النوع الصحيح، فسوف يثير استثناءً على الفور (بدلاً من الفشل فقط عند استخدام كائن التعليمات البرمجية لاحقًا).
أما بالنسبة لما تعنيه الحجج والسمات، فغالبًا ما يتم توثيق ذلك في جدول كبير الوثائق الخاصة بـ inspect
وحدة.إليك الجزء ذو الصلة:
code co_argcount number of arguments (not including * or ** args)
co_code string of raw compiled bytecode
co_consts tuple of constants used in the bytecode
co_filename name of file in which this code object was created
co_firstlineno number of first line in Python source code
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables
الصفات co_freevars
و co_cellvars
لم يتم توثيقها بقدر ما أستطيع أن أرى.أعتقد أنها مرتبطة بالإغلاقات.