12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Reserved words are off-limitsNames you define cannot be the same as rwords that mean special things in the<strong>Python</strong> language. For instance, if you try to use a variable name like class,<strong>Python</strong> will raise a syntax error, but klass and Class work fine. Table 11-3 liststhe words that are currently reserved (and hence off-limits) in <strong>Python</strong>.Table 11-3. <strong>Python</strong> reserved wordsand elif if passas (in 2.6 and later) else import printassert except in raisebreak exec is returnclass finally lambda trycontinue for nonlocal (in 3.0) whiledef from not with (in 2.6 and later)del global or yieldyield was an optional extension in <strong>Python</strong> 2.2, but is a standard keywordas of 2.3. It is used in conjunction with generator functions, anewer feature discussed in Chapter 17. This was one of a small handfulof instances where <strong>Python</strong> broke with backward compatibility.Still, yield was phased in over time—it began generating warnings in2.2, and was not enabled until 2.3.Similarly, in <strong>Python</strong> 2.6, the words with and as are scheduled tobecome new reserved words for use in context managers (a new formof exception handling). These two words are not reserved in 2.5,unless the context manager feature is turned on manually (seeChapter 27 for details). When used in 2.5, with and as generate warningsabout the upcoming change—except in the version of IDLE in<strong>Python</strong> 2.5, which appears to have enabled this feature for you (that is,using these words as variable names does generate errors in 2.5, butonly in its version of the IDLE GUI).<strong>Python</strong>’s reserved words are always all lowercase, and they are truly reserved; unlikenames in the built-in scope that you will meet in the next part of this book, you cannotredefine reserved words by assignment (e.g., and = 1 results in a syntax error). *Furthermore, because module names in import statements become variables in yourscript, this constraint extends to your module filenames—you can code files calledand.py and my-code.py, but you cannot import them because their names withoutthe “.py” extension become variables in your code, and so must follow all the variablerules just outlined (reserved words are off-limits, and dashes won’t work,though underscores will). We’ll revisit this idea in Part V.* In the Jython Java-based implementation of <strong>Python</strong>, though, user-defined variable names can sometimes bethe same as <strong>Python</strong> reserved words.226 | Chapter 11: Assignment, Expressions, and print

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

Saved successfully!

Ooh no, something went wrong!