2009-07-26 11 views
10

है, मैं कुछ उदाहरणों को सम्मिलित करने के लिए अंतर्निहित मॉड्यूल का उपयोग कर रहा हूं, इसलिए उन्हें डिबगिंग उद्देश्यों के लिए विश्व स्तर पर एक्सेस किया जा सकता है। __builtins__ मॉड्यूल के साथ समस्या यह है कि यह एक मुख्य लिपि में एक मॉड्यूल है और मॉड्यूल में एक dict है, लेकिन के रूप में मामलों के आधार पर मेरी स्क्रिप्ट एक मुख्य स्क्रिप्ट या एक मॉड्यूल हो सकता है, मैं यह करने के लिए है:क्यों __builtins__ दोनों मॉड्यूल और dict

if isinstance(__builtins__, dict): 
    __builtins__['g_frame'] = 'xxx' 
else: 
    setattr(__builtins__, 'g_frame', 'xxx') 

क्या कोई कामकाज है, इससे कम? सबसे महत्वपूर्ण बात यह है कि __builtins__ इस तरह से व्यवहार क्यों करता है?

यह देखने के लिए यहां एक स्क्रिप्ट है।

#module-a 
import b 
print 'a-builtin:',type(__builtins__) 

एक मॉड्यूल b.py बनाएं: एक मॉड्यूल a.py बनाएं

#module-b 
print 'b-builtin:',type(__builtins__) 

अब चलाने अजगर a.py:

$ python a.py 
b-builtin: <type 'dict'> 
a-builtin: <type 'module'> 
+0

अधिक जानकारी के लिए, देखें http://stackoverflow.com/questions/11181519/python-whats-the-difference-between-builtin-and-builtins [संभावित डुप्लिकेट] – pd12

उत्तर

11

मुझे लगता है कि आप __builtin__ मॉड्यूल चाहते हैं (एकवचन नोट करें)।

डॉक्स देखें:

27.3. __builtin__ — Built-in objects

CPython implementation detail: Most modules have the name __builtins__ (note the 's') made available as part of their globals. The value of __builtins__ is normally either this module or the value of this modules’s [sic] __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.

+0

पायथन 3 के बारे में क्या? 'नाम त्रुटि: नाम' __builtin__ 'परिभाषित नहीं किया गया है' – warvariuc

+0

@warvariuc: मॉड्यूल का नाम बदलकर ['buildins'] (https://docs.python.org/3/library/builtins.html) कर दिया गया था। –

संबंधित मुद्दे