यह व्यवहार अजगर 3. में तय किया गया है आप एक सूची समझ [i(0) + i(1) for a in alist]
का उपयोग करते हैं तो आप अपने आसपास के दायरे जो i
के लिए सुलभ है में a
परिभाषित करेगा। एक नए सत्र में list(i(0) + i(1) for a in alist)
त्रुटि फेंक देगा।
>>> i = lambda x: a[x]
>>> alist = [(1, 2), (3, 4)]
>>> list(i(0) + i(1) for a in alist)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
File "<stdin>", line 1, in <lambda>
NameError: global name 'a' is not defined
एक सूची समझ एक जनरेटर नहीं है: Generator expressions and list comprehensions।
Generator expressions are surrounded by parentheses (“()”) and list comprehensions are surrounded by square brackets (“[]”).
अपने उदाहरण list()
के रूप में एक वर्ग चर का अपना ही गुंजाइश है और यह अधिक से अधिक वैश्विक चर तक पहुँच गया है में। जब आप इसका उपयोग करते हैं, i
उस दायरे के अंदर a
देखेंगे। नए सत्र में इस प्रयास करें:
>>> i = lambda x: a[x]
>>> alist = [(1, 2), (3, 4)]
>>> [i(0) + i(1) for a in alist]
[3, 7]
>>> a
(3, 4)
यह करने के लिए यह तुलना एक और सत्र में:
>>> i = lambda x: a[x]
>>> alist = [(1, 2), (3, 4)]
>>> l = (i(0) + i(1) for a in alist)
<generator object <genexpr> at 0x10e60db90>
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> [x for x in l]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
File "<stdin>", line 1, in <lambda>
NameError: global name 'a' is not defined
जब आप list(i(0) + i(1) for a in alist)
चलाने आप list
वर्ग है जो यह यह करने के लिए कनवर्ट करने की कोशिश करेंगे के लिए एक जनरेटर (i(0) + i(1) for a in alist)
पारित करेंगे सूची वापस करने से पहले अपने दायरे में एक सूची। इस जनरेटर के लिए जिसमें लैम्ब्डा फ़ंक्शन के अंदर कोई पहुंच नहीं है, परिवर्तनीय a
का कोई अर्थ नहीं है।
जनरेटर ऑब्जेक्ट <generator object <genexpr> at 0x10e60db90>
ने परिवर्तनीय नाम a
खो दिया है। फिर जब list
जनरेटर को कॉल करने का प्रयास करता है, तो लैम्ब्डा फ़ंक्शन अपरिभाषित a
के लिए त्रुटि फेंक देगा।
जनरेटर के साथ विपरीत में सूची comprehensions का व्यवहार भी here उल्लेख किया:
List comprehensions also "leak" their loop variable into the surrounding scope. This will also change in Python 3.0, so that the semantic definition of a list comprehension in Python 3.0 will be equivalent to list(). Python 2.4 and beyond should issue a deprecation warning if a list comprehension's loop variable has the same name as a variable used in the immediately surrounding scope.
python3 में:
>>> i = lambda x: a[x]
>>> alist = [(1, 2), (3, 4)]
>>> [i(0) + i(1) for a in alist]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "<stdin>", line 1, in <lambda>
NameError: name 'a' is not defined
यह मेरे लिए त्रुटि दिखाता है। –
@AvinashRaj दूसरी विधि को चलाने से पहले 'नाम त्रुटि: वैश्विक नाम' ए 'परिभाषित नहीं किया गया है' –
आपकी समस्या है, पहली बार (3, 4) के रूप में परिभाषित किया जाता है, फिर सूची() फ़ंक्शन हमेशा लेता है 'a' – TheGeorgeous