Skip to content

Commit

Permalink
Notes from future import annotations, from_points, property
Browse files Browse the repository at this point in the history
  • Loading branch information
Loic Riegel committed Mar 26, 2024
1 parent b235d9a commit 399667c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
22 changes: 16 additions & 6 deletions TP3/Correction/vectors.py
@@ -1,4 +1,13 @@
"""Manipulation of 2D-vectors."""
"""Manipulation of 2D-vectors.
Note :
``from __future__ import annotations`` est nécessaire si l'on veut pouvoir utiliser une classe
en tant qu'annotation de type dans cette même classe ! Comme dans l'exemple suivant :
class Point:
@classmethod
def origin(cls) -> Point: # "Error: Point not defined" serait levé sans l'utilisation de ``from __future__ import annotations``
"""

from __future__ import annotations

Expand All @@ -24,9 +33,10 @@ def __init__(self, dx: float, dy: float) -> None:
self.dy = dx

@classmethod
def from_two_points(cls, tail: Point, head: Point) -> Vector:
def from_points(cls, tail: Point, head: Point) -> Vector:
return Vector(head.x - tail.x, head.y - head.y)

@property
def magnitude(self) -> float:
return math.sqrt(self.dx**2 + self.dy**2)

Expand Down Expand Up @@ -54,7 +64,7 @@ def main_2():
v = Vector(1, 1)
v2 = Vector(3, -2)

print(f"{v.magnitude() = }")
print(f"{v.magnitude = }")
print(f"{v.dot_prod(v2) = }")
print(f"{v2.dot_prod(v) = }")

Expand All @@ -76,9 +86,9 @@ def main_4():
point_A = Point(-2, -1)
point_B = Point(3, 7)

vector_AB = Vector.from_two_points(point_A, point_B)
vector_OA = Vector.from_two_points(origin, point_A)
vector_AB = Vector.from_two_points(point_A, point_B)
vector_AB = Vector.from_points(point_A, point_B)
vector_OA = Vector.from_points(origin, point_A)
vector_AB = Vector.from_points(point_A, point_B)

print(f"Vector OA: dx={vector_OA.x} dy={vector_OA.y}")
print(f"Vector AB: dx={vector_AB.x} dy={vector_AB.y}")
Expand Down
4 changes: 2 additions & 2 deletions TP3/README.md
Expand Up @@ -81,10 +81,10 @@ On souhaite pouvoir créer un vecteur en donnant à l'initialisation seulement l
On souhaite que ce code fonctionne :
```py
origin = Point.origin() # Point(0, 0)
vector_OB = Vector.from_two_points(point_A, point_B)
vector_OB = Vector.from_points(point_A, point_B)
```

En utilisant une **méthode de class**, implémenter la méthode `origin` sur la classe `Point` et `from_two_points` sur la classe `Vector`.
En utilisant une **méthode de class**, implémenter la méthode `origin` sur la classe `Point` et `from_points` sur la classe `Vector`.


## Exercice 4
Expand Down
8 changes: 4 additions & 4 deletions TP3/vectors.py
Expand Up @@ -17,7 +17,7 @@ def main_2():
v = Vector(1, 1)
v2 = Vector(3, -2)

print(f"{v.magnitude() = }")
print(f"{v.magnitude() = }") # or v.magnitude if using the @property decorator
print(f"{v.dot_prod(v2) = }")
print(f"{v2.dot_prod(v) = }")

Expand All @@ -39,9 +39,9 @@ def main_4():
point_A = Point(-2, -1)
point_B = Point(3, 7)

vector_AB = Vector.from_two_points(point_A, point_B)
vector_OA = Vector.from_two_points(origin, point_A)
vector_AB = Vector.from_two_points(point_A, point_B)
vector_AB = Vector.from_points(point_A, point_B)
vector_OA = Vector.from_points(origin, point_A)
vector_AB = Vector.from_points(point_A, point_B)

print(f"Vector OA: dx={vector_OA.x} dy={vector_OA.y}")
print(f"Vector AB: dx={vector_AB.x} dy={vector_AB.y}")
Expand Down

0 comments on commit 399667c

Please sign in to comment.