Mercurial > js4py
annotate source/javascript-objects.rst @ 211:4e6cfa066d1a
Use double backticks for inline code everywhere
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Fri, 18 May 2012 16:56:15 +0200 |
| parents | 454054afcc0a |
| children |
| rev | line source |
|---|---|
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
1 JavaScript objects |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
2 ================== |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
3 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
4 Like Python classes |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
5 ------------------- |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
7 In JavaScript all objects are part of an inheritance tree. The |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
8 ``create`` function adds a node to the inheritance tree. |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
9 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
10 .. code-block:: javascript |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
11 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
12 // A JavaScript object. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
13 js> root = {} |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
14 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
15 // Prototype inheritance. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
16 js> create = function (obj) { |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
17 var f = function () {return this;}; |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
18 f.prototype = obj; |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
19 return new f; |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
20 } |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
21 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
22 js> a = create(root) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
23 js> b = create(a) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
24 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
25 js> a.name = 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
26 js> a.name |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
27 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
28 js> b.name |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
29 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
30 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
31 In Python classes inherit in the same way. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
32 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
33 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
34 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
35 >>> root = type # Most classes are instance of type. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
36 >>> class a(root): pass |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
37 >>> class b(a): pass # Class inheritance. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
38 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
39 >>> a.name = 5 # Just like JavaScript. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
40 >>> a.name |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
41 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
42 >>> b.name |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
43 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
44 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
45 class explanation |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
46 ^^^^^^^^^^^^^^^^^ |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
47 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
48 In Python we can subclass anything whose type is ``type`` (or a |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
49 subclass of type). A subclass (and its instances) inherits properties |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
50 from the super-class. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
51 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
52 >>> type(root) == type(a) == type(b) == type |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
53 True |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
54 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
55 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
56 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
57 Custom item methods |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
58 ------------------- |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
59 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
60 In JavaScript attribute and item access are the same. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
61 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
62 .. code-block:: javascript |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
63 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
64 js> a = create(root) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
65 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
66 js> a.name = 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
67 js> a['name'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
68 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
69 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
70 js> a['key'] = 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
71 js> a.key |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
72 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
73 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
74 js> a[1] = 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
75 js> a['1'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
76 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
77 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
78 In Python we can define our own item methods. (The programmer owns |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
79 the dot.) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
80 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
81 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
82 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
83 >>> class A(object): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
84 ... |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
85 ... def __getitem__(self, key): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
86 ... return getattr(self, str(key)) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
87 ... def __setitem__(self, key, value): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
88 ... return setattr(self, str(key), value) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
89 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
90 >>> a = A() |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
91 >>> a.name = 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
92 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
93 >>> a['name'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
94 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
95 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
96 >>> a['key'] = 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
97 >>> a.key |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
98 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
99 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
100 >>> a[1] = 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
101 >>> a['1'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
102 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
103 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
104 Because ``type(a)`` is ``A``, which has the special item methods, we |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
105 get the special item behaviour. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
106 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
107 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
108 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
109 >>> type(a) is A |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
110 True |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
111 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
112 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
113 On metaclass |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
114 ------------ |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
115 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
116 Using previous definition, we cannot subclass ``a`` to create ``b``. |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
117 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
118 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
119 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
120 >>> class b(a): pass |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
121 Traceback (most recent call last): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
122 class b(a): pass |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
123 TypeError: Error when calling the metaclass bases |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
124 object.__new__() takes no parameters |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
125 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
126 This is because ``a`` is not a type. The solution involves Python |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
127 metaclasses (an advanced topic). |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
128 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
129 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
130 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
131 >>> isinstance(a, type) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
132 False |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
133 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
134 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
135 metaclass construction |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
136 ^^^^^^^^^^^^^^^^^^^^^^ |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
137 We will subclass type, not object, and add to it the special item |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
138 methods. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
139 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
140 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
141 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
142 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
143 >>> class ObjectType(type): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
144 ... |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
145 ... def __getitem__(self, key): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
146 ... return getattr(self, str(key)) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
147 ... |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
148 ... def __setitem__(self, key, value): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
149 ... return setattr(self, str(key), value) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
150 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
151 Here is a fancy way of calling **ObjectType**. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
152 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
153 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
154 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
155 >>> class root(object): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
156 ... __metaclass__ = ObjectType |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
157 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
158 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
159 Here is a more direct (and equivalent) construction (create an |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
160 instance of ``ObjectType``, whose instances are objects). |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
161 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
162 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
163 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
164 >>> root = ObjectType('root', (object,), {}) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
165 >>> isinstance(root(), object) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
166 True |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
167 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
168 metaclass demonstration |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
169 ^^^^^^^^^^^^^^^^^^^^^^^ |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
170 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
171 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
172 >>> class a(root): pass |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
173 >>> class b(a): pass |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
174 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
175 >>> a.name = 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
176 >>> a.name |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
177 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
178 >>> b.name |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
179 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
180 >>> a['name'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
181 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
182 >>> b['name'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
183 5 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
184 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
185 >>> a[1] = 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
186 >>> a['1'] |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
187 6 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
188 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
189 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
190 metaclass explanation |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
191 ^^^^^^^^^^^^^^^^^^^^^ |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
192 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
193 Because ``type(root)`` is a subclass of type we can subclass root. |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
194 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
195 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
196 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
197 >>> issubclass(type(root), type) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
198 True |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
199 |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
200 Because the ``type(root)`` is ``ObjectType``, which has special item |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
201 methods, we get the special item behaviour. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
202 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
203 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
204 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
205 >>> type(root) == type(a) == type(b) == ObjectType |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
206 True |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
207 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
208 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
209 Never instantiated |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
210 ------------------ |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
211 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
212 We can't call JavaScript objects (unless they are a function). But |
|
211
4e6cfa066d1a
Use double backticks for inline code everywhere
Radomir Dopieralski <radomir.dopieralski@allegro.pl>
parents:
94
diff
changeset
|
213 ``create`` creates ordinary JavaScript objects. |
|
94
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
214 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
215 .. code-block:: javascript |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
216 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
217 js> a = create(root) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
218 js> a(1, 2, 3) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
219 TypeError: a is not a function |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
220 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
221 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
222 We will monkey-patch the previous Python class, to provide custom |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
223 behaviour when called. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
224 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
225 .. doctest:: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
226 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
227 >>> def raise_not_a_function(obj, *argv, **kwargs): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
228 ... raise TypeError, obj.__name__ + ' is not a function' |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
229 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
230 >>> ObjectType.__call__ = raise_not_a_function |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
231 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
232 >>> a(1, 2, 3) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
233 Traceback (most recent call last): |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
234 a(1, 2, 3) |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
235 TypeError: a is not a function |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
236 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
237 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
238 Conclusion |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
239 ---------- |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
240 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
241 JavaScript objects are like Python classes (because they inherit like |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
242 Python classes). |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
243 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
244 For JavaScript attribute and item access are the same. This is |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
245 achieved in Python by providing custom item methods. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
246 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
247 In Python the custom item methods must be placed on the type of the |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
248 object (or a superclass of its type). |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
249 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
250 Ordinary JavaScript objects are not functions and cannot be called. A |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
251 Python class can be called (to create an instance of the object). But |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
252 we can override this behaviour by supplying a custom method for call. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
253 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
254 To summarize: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
255 .. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
256 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
257 **JavaScript objects are like Python classes with custom item |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
258 methods (on the metaclass) that are never instantiated.** |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
259 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
260 It's worth saying again: |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
261 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
262 .. |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
263 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
264 **JavaScript objects are like Python classes with custom item |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
265 methods (on the metaclass) that are never instantiated.** |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
266 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
267 |
|
454054afcc0a
$ cp -piv ../python-classes-tutorial/source/javascript-objects.rst source/ # and some other changes
Jonathan Fine <jfine@pytex.org>
parents:
diff
changeset
|
268 |
