|
1 | 1 | .. _tut-classes:
|
2 | 2 |
|
3 |
| -******* |
4 |
| -Classes |
5 |
| -******* |
6 |
| - |
7 |
| -Python's class mechanism adds classes to the language with a minimum of new |
8 |
| -syntax and semantics. It is a mixture of the class mechanisms found in C++ and |
9 |
| -Modula-3. As is true for modules, classes in Python do not put an absolute |
10 |
| -barrier between definition and user, but rather rely on the politeness of the |
11 |
| -user not to "break into the definition." The most important features of classes |
12 |
| -are retained with full power, however: the class inheritance mechanism allows |
13 |
| -multiple base classes, a derived class can override any methods of its base |
14 |
| -class or classes, and a method can call the method of a base class with the same |
15 |
| -name. Objects can contain an arbitrary amount of data. |
16 |
| - |
17 |
| -In C++ terminology, all class members (including the data members) are *public*, |
18 |
| -and all member functions are *virtual*. As in Modula-3, there are no shorthands |
19 |
| -for referencing the object's members from its methods: the method function is |
20 |
| -declared with an explicit first argument representing the object, which is |
21 |
| -provided implicitly by the call. As in Smalltalk, classes themselves are |
22 |
| -objects. This provides semantics for importing and renaming. Unlike C++ and |
23 |
| -Modula-3, built-in types can be used as base classes for extension by the user. |
24 |
| -Also, like in C++, most built-in operators with special syntax (arithmetic |
25 |
| -operators, subscripting etc.) can be redefined for class instances. |
26 |
| - |
27 |
| -(Lacking universally accepted terminology to talk about classes, I will make |
28 |
| -occasional use of Smalltalk and C++ terms. I would use Modula-3 terms, since |
29 |
| -its object-oriented semantics are closer to those of Python than C++, but I |
30 |
| -expect that few readers have heard of it.) |
31 |
| - |
| 3 | +***** |
| 4 | +Класс |
| 5 | +***** |
| 6 | + |
| 7 | +Пайтон нь маш багахан синтакс болон семантикийн ойлголтыг ашиглан классыг |
| 8 | +нэвтрүүлсэн. Хатуу синтакс, семантик ашиглан классыг тодорхойлохын оронд |
| 9 | +хөгжүүлэгчид тодорхойлолтыг эвдэлгүй ажлаа хийхийг санал болгодог. Классын |
| 10 | +талаарх бүхий л чухал ойлголтуудыг хэрэгжүүлсэн. Мөн удамшуулах механизм нь |
| 11 | +олон эцэг класстай байхыг зөвшөөрч тэдгээрийн аль ч функцийг нь дахин тодорхойлох |
| 12 | +боломжийг олгодог. Эцэг классын аль ч функцийг дуудаж болно. |
| 13 | + |
| 14 | +C++, Modula-3, Smalltalk хэлнүүдийн зарим онцлог шинжүүдийг өөртөө шингээсэн. |
| 15 | +Бүх гишүүн өгөгдлүүд нь нийтийн. Объект нь өөрийгөө заах хялбархан заагчгүй |
| 16 | +функцийн зарлагаанд нь эхний аргументаараа объектоо дамжуулдаг. Объектийн класс |
| 17 | +нь Пайтонд объект төрлөөр хэрэглэгддэг. Мөн дээр дурдсан хэлнүүдээс ялгарах |
| 18 | +онцлог нь дотоод өгөгдлийн төрлөө эцэг класс болгож удамжуулж тусгай функцүүдийг |
| 19 | +нь дахин тодорхойлсноор энгийн операторуудад ашиглагддаг функцийн ажиллагааг |
| 20 | +өөрчилж болдог. |
32 | 21 |
|
33 | 22 | .. _tut-object:
|
34 | 23 |
|
35 |
| -A Word About Names and Objects |
36 |
| -============================== |
37 |
| - |
38 |
| -Objects have individuality, and multiple names (in multiple scopes) can be bound |
39 |
| -to the same object. This is known as aliasing in other languages. This is |
40 |
| -usually not appreciated on a first glance at Python, and can be safely ignored |
41 |
| -when dealing with immutable basic types (numbers, strings, tuples). However, |
42 |
| -aliasing has a possibly surprising effect on the semantics of Python code |
43 |
| -involving mutable objects such as lists, dictionaries, and most other types. |
44 |
| -This is usually used to the benefit of the program, since aliases behave like |
45 |
| -pointers in some respects. For example, passing an object is cheap since only a |
46 |
| -pointer is passed by the implementation; and if a function modifies an object |
47 |
| -passed as an argument, the caller will see the change --- this eliminates the |
48 |
| -need for two different argument passing mechanisms as in Pascal. |
| 24 | +Утга олгох ба Объект |
| 25 | +==================== |
49 | 26 |
|
| 27 | +Объектуудыг алиас үүсгэх гэсэн утгаар нь хувьсагчид хадгалж болно. Өөрчлөх |
| 28 | +боломжгүй төрөл болох тэмдэгт мөр, тюпл ашиглах үед өөр хувьсагчид шилжүүлэхийг |
| 29 | +анзаарахгүй байж болно. Гэвч өөрчилж болох төрлүүдийг ашиглах үед энэ нь |
| 30 | +заагчийг дамжуулдаг учраас ашиглахад илүү дөхөм болно. Жишээ нь объектийг |
| 31 | +дамжуулах үйлдэл нь зөвхөн заагчийг хооронд нь хуулбарладаг учраас маш хямдхан |
| 32 | +үйлдэл болдог. Хэрвээ функцийн аргументаар объектийг нь өгсөн тохиолдолд дуудалтаас |
| 33 | +буцаж ирэх үед өгсөн объект маань өөрчлөгдөх боломжтой. |
50 | 34 |
|
51 | 35 | .. _tut-scopes:
|
52 | 36 |
|
53 |
| -Python Scopes and Namespaces |
54 |
| -============================ |
| 37 | +Нэрийн муж, хүрээ |
| 38 | +================= |
55 | 39 |
|
56 |
| -Before introducing classes, I first have to tell you something about Python's |
57 |
| -scope rules. Class definitions play some neat tricks with namespaces, and you |
58 |
| -need to know how scopes and namespaces work to fully understand what's going on. |
59 |
| -Incidentally, knowledge about this subject is useful for any advanced Python |
60 |
| -programmer. |
| 40 | +Классын талаар танилцуулахын өмнө Пайтонгийн локал глобал хамрах хүрээний |
| 41 | +талаар мэдэх нь зүйтэй. Классын тодорхойлолт нь нэрийн муж, хамрах хүрээтэй |
| 42 | +нягт хамтарч ажилладаг. Яаж ажиллаж байгааг нь сайтар мэдсэнээр ахисан |
| 43 | +шатны Пайтон хөгжүүлэгч болно. |
61 | 44 |
|
62 | 45 | Let's begin with some definitions.
|
63 | 46 |
|
64 |
| -A *namespace* is a mapping from names to objects. Most namespaces are currently |
65 |
| -implemented as Python dictionaries, but that's normally not noticeable in any |
66 |
| -way (except for performance), and it may change in the future. Examples of |
67 |
| -namespaces are: the set of built-in names (functions such as :func:`abs`, and |
68 |
| -built-in exception names); the global names in a module; and the local names in |
69 |
| -a function invocation. In a sense the set of attributes of an object also form |
70 |
| -a namespace. The important thing to know about namespaces is that there is |
71 |
| -absolutely no relation between names in different namespaces; for instance, two |
72 |
| -different modules may both define a function ``maximize`` without confusion --- |
73 |
| -users of the modules must prefix it with the module name. |
74 |
| - |
75 |
| -By the way, I use the word *attribute* for any name following a dot --- for |
76 |
| -example, in the expression ``z.real``, ``real`` is an attribute of the object |
77 |
| -``z``. Strictly speaking, references to names in modules are attribute |
78 |
| -references: in the expression ``modname.funcname``, ``modname`` is a module |
79 |
| -object and ``funcname`` is an attribute of it. In this case there happens to be |
80 |
| -a straightforward mapping between the module's attributes and the global names |
81 |
| -defined in the module: they share the same namespace! [#]_ |
82 |
| - |
83 |
| -Attributes may be read-only or writable. In the latter case, assignment to |
84 |
| -attributes is possible. Module attributes are writable: you can write |
85 |
| -``modname.the_answer = 42``. Writable attributes may also be deleted with the |
86 |
| -:keyword:`del` statement. For example, ``del modname.the_answer`` will remove |
87 |
| -the attribute :attr:`the_answer` from the object named by ``modname``. |
| 47 | +*Нэрийн муж* гэдэг нь нэрийг объектуудтай харьцуулсан буулгалт. Ихэнх нэрийн |
| 48 | +мужнууд нь толь төрөлтэй адилаар хэрэгжүүлэгдсэн байдаг ба ажиллах явцад түүнийг |
| 49 | +ажиглаж боломжгүй байдаг. Жишээ нь Пайтонгийн дотоод нэрийн муж, модуль дах |
| 50 | +глобал нэрүүд, функц доторх локал нэрнүүд гэх мэтчилэн. Объектын аттрибутууд |
| 51 | +нь нэрийн муж үүсгэнэ. Өөр өөр нэрийн мужид байрлаж байгаа нэрнүүд нь хоорондоо |
| 52 | +огтхон ч хамааралгүй байдаг. Өөрөөр хэлбэл 2 өөр модуль ижилхэн ``maximize`` |
| 53 | +функцийг зарлаж болно. Ингэж зарласан функцийг ашиглахын тулд модулийн нэрээр |
| 54 | +нь угтвар хийж дуудаж ялгах хэрэгтэй. |
| 55 | + |
| 56 | +*атрибут* гэж цэгийн дараа үргэлжлүүлэн бичсэн нэрийг тэмдэглэсэн болно. Ө.Х |
| 57 | +``z.real``, ``real`` гэдэг нь ``z`` объектын атрибут юм. Модуль доторх нэрнүүд |
| 58 | +рүү хандаж байгаа нь модулийн атрибут руу хандаж байгаа гэсэн үг. ``modname.funcname`` |
| 59 | +гэвэл ``modname`` нь объект, харин ``funcname`` нь түүний атрибут болно. |
| 60 | + |
| 61 | +Атрибутуудыг зөвхөн уншиж эсвэл уншиж өөрчилж болно. Атрибутад утга оноож |
| 62 | +болно. Модулийн атрибутуудыг ``modname.the_answer = 42`` гэж өөрчилж болдог. |
| 63 | +Өөрчилж болдог атрибутыг мөн :keyword:`del` оператороор устгах буюу хасаж |
| 64 | +болно. Жишээлбэл ``del modname.the_answer`` гэвэл ``modname`` объектоос |
| 65 | +:attr:`the_answer` гэдэг нэрийг хасна. |
88 | 66 |
|
89 | 67 | Namespaces are created at different moments and have different lifetimes. The
|
90 | 68 | namespace containing the built-in names is created when the Python interpreter
|
|
0 commit comments