Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions concepts/classes/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ class Demo:
The moment that `<object>.add_two()` is called, and `self.new_var += 2` is read, `new_var` changes from a class variable to an instance variable of the same name.

This can be useful during initialization when all instances of a class will need some attribute(s) to start with the same value.
However, the instance variable then shadows* the class variable, making the class variable inaccessible from the instance where it is shadowed.
However, the instance variable then [_shadows_](https://oznetnerd.com/2017/07/17/python-shadowing/) the class variable, making the class variable inaccessible from the instance where it is shadowed.
Given this situation, it may be safer and clearer to set instance attributes from the `__init__()` method as `self.<attribute>`.
~~~~
_*[_shadows_][shadowing]


## Methods

Expand Down Expand Up @@ -240,7 +240,7 @@ class MyClass:
def change_location(self, amount):
self.location_x += amount
self.location_y += amount
return self.location_x, self.location_y
return self.location_x, self.location_y

# Make a new test_object with location (3,7)
>>> test_object = MyClass((3,7))
Expand All @@ -267,7 +267,7 @@ class MyClass:
def change_location(self, amount):
self.location_x += amount
self.location_y += amount
return self.location_x, self.location_y
return self.location_x, self.location_y

# Alter class variable number for all instances from within an instance.
def increment_number(self):
Expand Down Expand Up @@ -322,4 +322,3 @@ class MyClass:
[dunder]: https://mathspp.com/blog/pydonts/dunder-methods
[oop]: https://www.educative.io/blog/object-oriented-programming
[dot notation]: https://stackoverflow.com/questions/45179186/understanding-the-dot-notation-in-python
[shadowing]: https://oznetnerd.com/2017/07/17/python-shadowing/
4 changes: 2 additions & 2 deletions exercises/concept/ellens-alien-game/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
## 4. The `teleport` Method

- Remember that `object methods` are always passed `self` as the first parameter.
- Instance attributes can be updated from a method by using `self.<attribute>` = `<new attribute value>`.
- Instance attributes can be updated from a method by using `self.<attribute> = <new attribute value>`.

## 5. The `collision_detection` Method

Expand All @@ -39,4 +39,4 @@
- A `tuple` would be a _single_ parameter.
- The Alien constructor takes _2 parameters_.
- Unpacking what is _inside_ the tuple would yield two parameters.
- The standalone function is outside of the `class`
- The standalone function is outside of the `class`.
6 changes: 3 additions & 3 deletions exercises/concept/ellens-alien-game/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ It is up to you if `hit()` takes healths points _to_ or _below_ zero.

```python
>>> alien = Alien(0, 0)
>>> alien.health

# Initialized health value.
>>> alien.health
3

# Decrements health by 1 point.
Expand Down Expand Up @@ -103,8 +103,8 @@ For example:
2
>>> alien_one.total_aliens_created
2
>>> Alien.total_aliens_created
# Accessing the variable from the class directly
>>> Alien.total_aliens_created
2
```

Expand All @@ -120,7 +120,7 @@ For example:
>>> aliens = new_aliens_collection(alien_start_positions)
...
>>> for alien in aliens:
print(alien.x_coordinate, alien.y_coordinate)
... print(alien.x_coordinate, alien.y_coordinate)
(4, 7)
(-1, 0)
```
4 changes: 2 additions & 2 deletions exercises/concept/ellens-alien-game/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class MyClass:
def change_location(self, amount):
self.location_x += amount
self.location_y += amount
return self.location_x, self.location_y
return self.location_x, self.location_y

# Make a new test_object with location (3,7)
>>> test_object = MyClass((3,7))
Expand All @@ -209,7 +209,7 @@ class MyClass:
def change_location(self, amount):
self.location_x += amount
self.location_y += amount
return self.location_x, self.location_y
return self.location_x, self.location_y

# Alter class variable number for all instances from within an instance.
def increment_number(self):
Expand Down
18 changes: 9 additions & 9 deletions exercises/concept/ellens-alien-game/.meta/exemplar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Alien:

Attributes:
(class) total_aliens_created (int): Total number of Alien instances.
x_coordinate (int): Position on the x-axis.
x_coordinate (int): Position on the x-axis.
y_coordinate (int): Position on the y-axis.
health (int): Number of health points.
health (int): Number of health points.

Methods:
hit(): Decrement Alien health by one point.
Expand All @@ -29,9 +29,9 @@ def __init__(self, x_coordinate, y_coordinate):
health (int): Number of health points.

Attributes:
x_coordinate (int): Position on the x-axis.
x_coordinate (int): Position on the x-axis.
y_coordinate (int): Position on the y-axis.
health (int): Number of health points. Defaults to 3.
health (int): Number of health points. Defaults to 3.

Returns:
Alien (Alien Object): New Alien.
Expand Down Expand Up @@ -59,7 +59,7 @@ def is_alive(self):
"""Return if the Alien is alive.

Returns:
bool: Is the Alien Alive?
bool: Is the Alien Alive?
"""

return self.health > 0
Expand Down Expand Up @@ -94,11 +94,11 @@ def collision_detection(self, other):
def new_aliens_collection(positions):
"""Create a list of Alien instances from a list of coordinate tuples.

Parameters:
positions (list[tuple]): List of (x, y) coordinates in tuples..
Parameters:
positions (list[tuple]): List of (x, y) coordinates in tuples.

Returns:
list[object]: List of Alien objects.
Returns:
list[object]: List of Alien objects.
"""

return [Alien(position[0], position[1]) for position in positions]
6 changes: 3 additions & 3 deletions exercises/concept/ellens-alien-game/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Alien:

Attributes:
(class) total_aliens_created (int): Total number of Alien instances.
x_coordinate (int): Position on the x-axis.
x_coordinate (int): Position on the x-axis.
y_coordinate (int): Position on the y-axis.
health (int): Number of health points.
health (int): Number of health points.

Methods:
hit(): Decrement Alien health by one point.
Expand All @@ -21,5 +21,5 @@ class Alien:
pass


#TODO (Student): Create the new_aliens_collection() function below to call your Alien class with a list of coordinates
#TODO (Student): Create the new_aliens_collection() function below to call your Alien class with a list of coordinates

Loading