Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7b6908

Browse files
committed
Merged revisions 82262,82269,82434,82480-82481,82484-82485,82487-82488,82594,82599,82615 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k ................ r82262 | georg.brandl | 2010-06-27 12:17:12 +0200 (So, 27 Jun 2010) | 1 line #9078: fix some Unicode C API descriptions, in comments and docs. ................ r82269 | georg.brandl | 2010-06-27 12:59:19 +0200 (So, 27 Jun 2010) | 1 line Wording fix. ................ r82434 | georg.brandl | 2010-07-02 09:41:51 +0200 (Fr, 02 Jul 2010) | 9 lines Merged revisions 82433 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r82433 | georg.brandl | 2010-07-02 09:33:50 +0200 (Fr, 02 Jul 2010) | 1 line Grammar and markup fixes. ........ ................ r82480 | georg.brandl | 2010-07-03 12:21:50 +0200 (Sa, 03 Jul 2010) | 1 line Wrap and use the correct directive. ................ r82481 | georg.brandl | 2010-07-03 12:22:10 +0200 (Sa, 03 Jul 2010) | 1 line Use the right role. ................ r82484 | georg.brandl | 2010-07-03 12:26:17 +0200 (Sa, 03 Jul 2010) | 9 lines Recorded merge of revisions 82474 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r82474 | georg.brandl | 2010-07-03 10:40:13 +0200 (Sa, 03 Jul 2010) | 1 line Fix role name. ........ ................ r82485 | georg.brandl | 2010-07-03 12:26:54 +0200 (Sa, 03 Jul 2010) | 9 lines Merged revisions 82483 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r82483 | georg.brandl | 2010-07-03 12:25:54 +0200 (Sa, 03 Jul 2010) | 1 line Add link to bytecode docs. ........ ................ r82487 | georg.brandl | 2010-07-03 12:33:26 +0200 (Sa, 03 Jul 2010) | 1 line Fix markup. ................ r82488 | georg.brandl | 2010-07-03 12:41:33 +0200 (Sa, 03 Jul 2010) | 1 line Remove the need for a "()" empty argument list after opcodes. ................ r82594 | georg.brandl | 2010-07-05 22:13:41 +0200 (Mo, 05 Jul 2010) | 1 line Update Vec class constructor, remove indirection via function, use operator module. ................ r82599 | alexander.belopolsky | 2010-07-05 23:44:05 +0200 (Mo, 05 Jul 2010) | 1 line "Modernized" the demo a little. ................ r82615 | georg.brandl | 2010-07-07 00:58:50 +0200 (Mi, 07 Jul 2010) | 1 line Fix typo. ................
1 parent 1648924 commit c7b6908

9 files changed

Lines changed: 148 additions & 121 deletions

File tree

Demo/classes/Vec.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1-
# A simple vector class
1+
class Vec:
2+
""" A simple vector class
23
4+
Instances of the Vec class can be constructed from numbers
35
4-
def vec(*v):
5-
return Vec(*v)
6+
>>> a = Vec(1, 2, 3)
7+
>>> b = Vec(3, 2, 1)
68
9+
added
10+
>>> a + b
11+
Vec(4, 4, 4)
712
8-
class Vec:
13+
subtracted
14+
>>> a - b
15+
Vec(-2, 0, 2)
16+
17+
and multiplied by a scalar on the left
18+
>>> 3.0 * a
19+
Vec(3.0, 6.0, 9.0)
920
21+
or on the right
22+
>>> a * 3.0
23+
Vec(3.0, 6.0, 9.0)
24+
"""
1025
def __init__(self, *v):
1126
self.v = list(v)
1227

13-
def fromlist(self, v):
28+
@classmethod
29+
def fromlist(cls, v):
1430
if not isinstance(v, list):
1531
raise TypeError
16-
self.v = v[:]
17-
return self
32+
inst = cls()
33+
inst.v = v
34+
return inst
1835

1936
def __repr__(self):
20-
return 'vec(' + repr(self.v)[1:-1] + ')'
37+
args = ', '.join(repr(x) for x in self.v)
38+
return 'Vec({})'.format(args)
2139

2240
def __len__(self):
2341
return len(self.v)
@@ -27,28 +45,24 @@ def __getitem__(self, i):
2745

2846
def __add__(self, other):
2947
# Element-wise addition
30-
v = list(map(lambda x, y: x+y, self, other))
31-
return Vec().fromlist(v)
48+
v = [x + y for x, y in zip(self.v, other.v)]
49+
return Vec.fromlist(v)
3250

3351
def __sub__(self, other):
3452
# Element-wise subtraction
35-
v = list(map(lambda x, y: x-y, self, other))
36-
return Vec().fromlist(v)
53+
v = [x - y for x, y in zip(self.v, other.v)]
54+
return Vec.fromlist(v)
3755

3856
def __mul__(self, scalar):
3957
# Multiply by scalar
40-
v = [x*scalar for x in self.v]
41-
return Vec().fromlist(v)
58+
v = [x * scalar for x in self.v]
59+
return Vec.fromlist(v)
4260

61+
__rmul__ = __mul__
4362

4463

4564
def test():
46-
a = vec(1, 2, 3)
47-
b = vec(3, 2, 1)
48-
print(a)
49-
print(b)
50-
print(a+b)
51-
print(a-b)
52-
print(a*3.0)
65+
import doctest
66+
doctest.testmod()
5367

5468
test()

Doc/c-api/unicode.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ APIs:
328328
Coerce an encoded object *obj* to an Unicode object and return a reference with
329329
incremented refcount.
330330

331-
String and other char buffer compatible objects are decoded according to the
332-
given encoding and using the error handling defined by errors. Both can be
333-
*NULL* to have the interface use the default values (see the next section for
334-
details).
331+
:class:`bytes`, :class:`bytearray` and other char buffer compatible objects
332+
are decoded according to the given encoding and using the error handling
333+
defined by errors. Both can be *NULL* to have the interface use the default
334+
values (see the next section for details).
335335

336336
All other objects, including Unicode objects, cause a :exc:`TypeError` to be
337337
set.

Doc/glossary.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Glossary
6363
"intermediate language" is said to run on a :term:`virtual machine`
6464
that executes the machine code corresponding to each bytecode.
6565

66+
A list of bytecode instructions can be found in the documentation for
67+
:ref:`the dis module <bytecodes>`.
68+
6669
class
6770
A template for creating user-defined objects. Class definitions
6871
normally contain method definitions which operate on instances of the

0 commit comments

Comments
 (0)