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.

Qualification is really an expression that returns the value assigned to an attributename associated with an object. For example, the expression module2.sys in the previousexample fetches the value assigned to sys in module2. Similarly, if we have abuilt-in list object L, L.append returns the append method object associated with thatlist.So, what does attribute qualification do to the scope rules we studied in Chapter 16?Nothing, really: it’s an independent concept. When you use qualification to accessnames, you give <strong>Python</strong> an explicit object from which to fetch the specified names.The LEGB rule applies only to bare, unqualified names. Here are the rules:Simple variablesX means search for the name X in the current scopes (following the LEGB rule).QualificationX.Y means find X in the current scopes, then search for the attribute Y in theobject X (not in scopes).Qualification pathsX.Y.Z means look up the name Y in the object X, then look up Z in the object X.Y.GeneralityQualification works on all objects with attributes: modules, classes, C extensiontypes, etc.In Part VI, we’ll see that qualification means a bit more for classes (it’s also the placewhere something called inheritance happens), but, in general, the rules outlined hereapply to all names in <strong>Python</strong>.Imports Versus ScopesAs we’ve learned, it is never possible to access names defined in another module filewithout first importing that file. That is, you never automatically get to see names inanother file, regardless of the structure of imports or function calls in your program.A variable’s meaning is always determined by the locations of assignments in yoursource code, and attributes are always requested of an object explicitly.For example, consider the following two simple modules. The first, moda.py, definesa variable X global to code in its file only, along with a function that changes the globalX in this file:X = 88# My X: global to this file onlydef f( ):global XX = 99# Change this file's X# Cannot see names in other modulesModule Namespaces | 407

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

Saved successfully!

Ooh no, something went wrong!