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.

explored in Chapter 5; this type provides set operations as built-in tools. Set implementationsare fun to experiment with, but they are no longer strictly required in<strong>Python</strong> today.For another type subclassing example, see the implementation of the new bool typein <strong>Python</strong> 2.3: as mentioned earlier in the book, bool is a subclass of int with twoinstances (True and False) that behave like the integers 1 and 0, but inherit customstring-representation methods that display their names.Pseudoprivate Class AttributesIn Part IV, we learned that every name assigned at the top level of a module file isexported. By default, the same holds for classes—data hiding is a convention, andclients may fetch or change any class or instance attribute they like. In fact, attributesare all “public” and “virtual” in C++ terms; they’re all accessible everywhere, andlooked up dynamically at runtime. *That’s still true today. However, <strong>Python</strong> also supports the notion of name“mangling” (i.e., expansion) to localize some names in classes. Mangled names aresometimes misleadingly called “private attributes,” but really this is just a way tolocalize a name to the class that created it—name mangling does not prevent accessby code outside the class. This feature is mostly intended to avoid namespace collisionsin instances, not to restrict access to names in general; mangled names aretherefore better called “pseudoprivate” than “private.”Pseudoprivate names are an advanced and entirely optional feature, and you probablywon’t find them very useful until you start writing large class hierarchies in multiprogrammerprojects. But, because you may see this feature in other people’s code, youneed to be somewhat aware of it, even if you don’t use it yourself.Name Mangling OverviewHere’s how name mangling works: names inside a class statement that start withtwo underscores, but don’t end with two underscores, are automatically expanded toinclude the name of the enclosing class. For instance, a name like __X within a classnamed Spam is changed to _Spam_ _X automatically: the original name is prefixed witha single underscore, and the enclosing class’ name. Because the modified namecontains the name of the enclosing class, it’s somewhat unique; it won’t clash withsimilar names created by other classes in a hierarchy.* This tends to scare C++ people unnecessarily. In <strong>Python</strong>, it’s even possible to change or completely delete aclass method at runtime. On the other hand, almost nobody ever does in practical programs. As a scriptinglanguage, <strong>Python</strong> is more about enabling than restricting. Also, recall from our discussion of operator overloadingin Chapter 24 that _ _getattr_ _ and _ _setattr_ _ can be used to emulate privacy, but are generallynot used for this in practice.Pseudoprivate Class Attributes | 543

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

Saved successfully!

Ooh no, something went wrong!