Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6d0654bf
Commit
6d0654bf
authored
Jul 05, 2010
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update Vec class constructor, remove indirection via function, use operator module.
parent
613eb4f8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
14 deletions
+14
-14
Demo/classes/Vec.py
Demo/classes/Vec.py
+14
-14
No files found.
Demo/classes/Vec.py
View file @
6d0654bf
# A simple vector class
def
vec
(
*
v
):
return
Vec
(
*
v
)
import
operator
class
Vec
:
...
...
@@ -10,14 +8,16 @@ class Vec:
def
__init__
(
self
,
*
v
):
self
.
v
=
list
(
v
)
def
fromlist
(
self
,
v
):
@
classmethod
def
fromlist
(
cls
,
v
):
if
not
isinstance
(
v
,
list
):
raise
TypeError
self
.
v
=
v
[:]
return
self
inst
=
cls
()
inst
.
v
=
v
return
inst
def
__repr__
(
self
):
return
'
v
ec('
+
repr
(
self
.
v
)[
1
:
-
1
]
+
')'
return
'
V
ec('
+
repr
(
self
.
v
)[
1
:
-
1
]
+
')'
def
__len__
(
self
):
return
len
(
self
.
v
)
...
...
@@ -27,24 +27,24 @@ class Vec:
def
__add__
(
self
,
other
):
# Element-wise addition
v
=
list
(
map
(
lambda
x
,
y
:
x
+
y
,
self
,
other
))
return
Vec
()
.
fromlist
(
v
)
v
=
list
(
map
(
operator
.
add
,
self
,
other
))
return
Vec
.
fromlist
(
v
)
def
__sub__
(
self
,
other
):
# Element-wise subtraction
v
=
list
(
map
(
lambda
x
,
y
:
x
-
y
,
self
,
other
))
return
Vec
()
.
fromlist
(
v
)
v
=
list
(
map
(
operator
.
sub
,
self
,
other
))
return
Vec
.
fromlist
(
v
)
def
__mul__
(
self
,
scalar
):
# Multiply by scalar
v
=
[
x
*
scalar
for
x
in
self
.
v
]
return
Vec
()
.
fromlist
(
v
)
return
Vec
.
fromlist
(
v
)
def
test
():
a
=
v
ec
(
1
,
2
,
3
)
b
=
v
ec
(
3
,
2
,
1
)
a
=
V
ec
(
1
,
2
,
3
)
b
=
V
ec
(
3
,
2
,
1
)
print
(
a
)
print
(
b
)
print
(
a
+
b
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment