12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

have to import it in order to inspect it. Once you do, you can run a dir call to seewhich names are predefined:>>> import _ _builtin_ _>>> dir(_ _builtin_ _)['ArithmeticError', 'AssertionError', 'AttributeError','DeprecationWarning', 'EOFError', 'Ellipsis',...many more names omitted...'str', 'super', 'tuple', 'type', 'unichr', 'unicode','vars', 'xrange', 'zip']The names in this list constitute the built-in scope in <strong>Python</strong>; roughly the first halfare built-in exceptions, and the second half are built-in functions. Because <strong>Python</strong>automatically searches this module last in its LEGB lookup rule, you get all thenames in this list for free; that is, you can use them without importing any modules.Thus, there are two ways to refer to a built-in function—by taking advantage of theLEGB rule, or by manually importing the _ _builtin_ _ module:>>> zip # The normal way>>> import _ _builtin_ _ # The hard way>>> _ _builtin_ _.zipThe second of these approaches is sometimes useful in advanced work. The carefulreader might also notice that because the LEGB lookup procedure takes the firstoccurrence of a name that it finds, names in the local scope may override variables ofthe same name in both the global and built-in scopes, and global names may overridebuilt-ins. A function can, for instance, create a local variable called open byassigning to it:def hider( ):open = 'spam'...open('data.txt')# Local variable, hides built-in# This won't open a file now in this scope!However, this will hide the built-in function called open that lives in the built-in(outer) scope. It’s also usually a bug, and a nasty one at that, because <strong>Python</strong> will notissue a warning message about it (there are times in advanced programming whereyou may really want to replace a built-in name by redefining it in your code). ** Here’s another thing you can do in <strong>Python</strong> that you probably shouldn’t—because the names True and Falseare just variables in the built-in scope, it’s possible to reassign them with a statement like True = False.Youwon’t break the logical consistency of the universe in so doing! This statement merely redefines the wordTrue for the single scope in which it appears. For more fun, though, you could say _ _builtin_ _.True = False,to reset True to False for the entire <strong>Python</strong> process! This may be disallowed in the future (and it sends IDLEinto a strange panic state that resets the user code process). This technique is, however, useful for tool writerswho must change built-ins such as open to customized functions. Also, note that third-party tools such asPyChecker will warn about common programming mistakes, including accidental assignment to built-innames (this is known as “shadowing” a built-in in PyChecker).Scope Rules | 315

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!