2009-07-26 24 views
10

Birkaç örnek eklemek için yerleşik modülü kullanıyorum, böylece hata ayıklama amacıyla tüm dünyaya erişilebilir. __builtins__ modülü ile sorun o ana komut bir modül olduğu ve modüllerde dict olmakla durumlarda bağlı benim komut bir ana komut dosyası veya bir modül olabilir, ben bunu yapmak zorunda:neden __builtins__ modül ve dict hem de

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

Bundan daha kısa bir çözüm var mı? Daha önemlisi, __builtins__ neden bu şekilde davranıyor?

Bunu görmeniz için bir komut dosyası.

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

bir modül b.py oluşturun: bir modül a.py oluşturun

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

Şimdi koşmak piton 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 bkz:

dokümanlarını görün [Olası Kopyalama] – pd12

cevap

11

Sana (__builtin__ modülünü istediğini düşünüyorum tekil not al). Daha fazla bilgi için

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

Python3 nedir? NameError: name '__builtin__' tanımlı değil ' – warvariuc

+0

@warvariuc: modül ['builtins'] olarak yeniden adlandırıldı (https://docs.python.org/3/library/builtins.html). –

İlgili konular