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.

Local VariablesThe variable res inside intersect is what in <strong>Python</strong> is called a local variable—a namethat is visible only to code inside the function def, and exists only while the functionruns. In fact, because all names assigned in any way inside a function are classified aslocal variables by default, nearly all the names in intersect are local variables:• res is obviously assigned, so it is a local variable.• Arguments are passed by assignment, so seq1 and seq2 are, too.• The for loop assigns items to a variable, so the name x is also local.All these local variables appear when the function is called, and disappear when thefunction exits—the return statement at the end of intersect sends back the resultobject, but the name res goes away. To fully explore the notion of locals, though, weneed to move on to Chapter 16.Chapter SummaryThis chapter introduced the core ideas behind function definition—the syntax andoperation of the def and return statements, the behavior of function call expressions,and the notion and benefits of polymorphism in <strong>Python</strong> functions. As we saw,a def statement is executable code that creates a function object at runtime; when thefunction is later called, objects are passed into it by assignment (recall that assignmentmeans object reference in <strong>Python</strong>, which, as we learned in Chapter 6, reallymeans pointer internally), and computed values are sent back by return. We alsobegan exploring the concepts of local variables and scopes in this chapter, but we’llsave all the details on those topics for Chapter 16. First, though, a quick quiz.308 | Chapter 15: Function Basics

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

Saved successfully!

Ooh no, something went wrong!