Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9)

Side by Side Diff: Doc/tutorial/classes.rst

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/tools/sphinxext/susp-ignored.csv ('k') | Doc/whatsnew/2.5.rst » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 .. _tut-classes: 1 .. _tut-classes:
2 2
3 ******* 3 *******
4 Classes 4 Classes
5 ******* 5 *******
6 6
7 Compared with other programming languages, Python's class mechanism adds classes 7 Compared with other programming languages, Python's class mechanism adds classes
8 with a minimum of new syntax and semantics. It is a mixture of the class 8 with a minimum of new syntax and semantics. It is a mixture of the class
9 mechanisms found in C++ and Modula-3. Python classes provide all the standard 9 mechanisms found in C++ and Modula-3. Python classes provide all the standard
10 features of Object Oriented Programming: the class inheritance mechanism allows 10 features of Object Oriented Programming: the class inheritance mechanism allows
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 to change without notice. 587 to change without notice.
588 588
589 Since there is a valid use-case for class-private members (namely to avoid name 589 Since there is a valid use-case for class-private members (namely to avoid name
590 clashes of names with names defined by subclasses), there is limited support for 590 clashes of names with names defined by subclasses), there is limited support for
591 such a mechanism, called :dfn:`name mangling`. Any identifier of the form 591 such a mechanism, called :dfn:`name mangling`. Any identifier of the form
592 ``__spam`` (at least two leading underscores, at most one trailing underscore) 592 ``__spam`` (at least two leading underscores, at most one trailing underscore)
593 is textually replaced with ``_classname__spam``, where ``classname`` is the 593 is textually replaced with ``_classname__spam``, where ``classname`` is the
594 current class name with leading underscore(s) stripped. This mangling is done 594 current class name with leading underscore(s) stripped. This mangling is done
595 without regard to the syntactic position of the identifier, as long as it 595 without regard to the syntactic position of the identifier, as long as it
596 occurs within the definition of a class. 596 occurs within the definition of a class.
597
598 Name mangling is helpful for letting subclasses override methods without
599 breaking intraclass method calls. For example::
600
601 class Mapping:
602 def __init__(self, iterable):
603 self.items_list = []
604 self.__update(iterable)
605
606 def update(self, iterable):
607 for item in iterable:
608 self.items_list.append(item)
609
610 __update = update # private copy of original update() method
611
612 class MappingSubclass(Mapping):
613
614 def update(self, keys, values):
615 # provides new signature for update()
616 # but does not break __init__()
617 for item in zip(keys, values):
618 self.items_list.append(item)
619 597
620 Note that the mangling rules are designed mostly to avoid accidents; it still is 598 Note that the mangling rules are designed mostly to avoid accidents; it still is
621 possible to access or modify a variable that is considered private. This can 599 possible to access or modify a variable that is considered private. This can
622 even be useful in special circumstances, such as in the debugger. 600 even be useful in special circumstances, such as in the debugger.
623 601
624 Notice that code passed to ``exec()`` or ``eval()`` does not consider the 602 Notice that code passed to ``exec()`` or ``eval()`` does not consider the
625 classname of the invoking class to be the current class; this is similar to the 603 classname of the invoking class to be the current class; this is similar to the
626 effect of the ``global`` statement, the effect of which is likewise restricted 604 effect of the ``global`` statement, the effect of which is likewise restricted
627 to code that is byte-compiled together. The same restriction applies to 605 to code that is byte-compiled together. The same restriction applies to
628 ``getattr()``, ``setattr()`` and ``delattr()``, as well as when referencing 606 ``getattr()``, ``setattr()`` and ``delattr()``, as well as when referencing
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 845
868 846
869 .. rubric:: Footnotes 847 .. rubric:: Footnotes
870 848
871 .. [#] Except for one thing. Module objects have a secret read-only attribute c alled 849 .. [#] Except for one thing. Module objects have a secret read-only attribute c alled
872 :attr:`__dict__` which returns the dictionary used to implement the module's 850 :attr:`__dict__` which returns the dictionary used to implement the module's
873 namespace; the name :attr:`__dict__` is an attribute but not a global name. 851 namespace; the name :attr:`__dict__` is an attribute but not a global name.
874 Obviously, using this violates the abstraction of namespace implementation, a nd 852 Obviously, using this violates the abstraction of namespace implementation, a nd
875 should be restricted to things like post-mortem debuggers. 853 should be restricted to things like post-mortem debuggers.
876 854
OLDNEW
« no previous file with comments | « Doc/tools/sphinxext/susp-ignored.csv ('k') | Doc/whatsnew/2.5.rst » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+