Skip to content

Commit ba8cffd

Browse files
authored
[Handshake and Clock]: Corrected Formatting & Edited Instructions Appends for Clarity (#4216)
* Corrected formatting and edited instructions append for clarity. * Update exercises/practice/clock/.docs/instructions.append.md
1 parent 4529546 commit ba8cffd

2 files changed

Lines changed: 47 additions & 23 deletions

File tree

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# Instructions append
22

3-
The tests for this exercise expect your clock will be implemented via a Clock `class` in Python.
4-
If you are unfamiliar with classes in Python, [classes][classes in python] from the Python docs is a good place to start.
3+
4+
## How this exercise is implemented for the Python track
5+
6+
7+
The tests for this exercise expect that your clock will be implemented in a Clock `class`.
8+
If you are unfamiliar with classes in Python, [concept:python/classes]() and [classes][classes in python] (_from the Python docs_) are good places to start.
9+
10+
511
## Representing your class
612

7-
When working with and debugging objects, it's important to have a string representation of that object.
8-
For example, if you were to create a new `datetime.datetime` object in the Python [REPL][REPL] environment, you could see its string representation:
13+
When working with and debugging [objects][what-is-an-object], it's important to have a good representation of that object.
14+
For example — if you were to create a new [`datetime.datetime`][datetime] object in the Python [REPL][REPL] environment, you could view its [string representation][str-rep-classes]:
15+
916

1017
```python
1118
>>> from datetime import datetime
@@ -14,57 +21,71 @@ For example, if you were to create a new `datetime.datetime` object in the Pytho
1421
datetime.datetime(2022, 5, 4, 0, 0)
1522
```
1623

17-
Your Clock `class` will create a custom `object` that handles times without dates.
24+
Your Clock `class` should create a custom `object` that handles times _without_ dates.
1825
One important aspect of this `class` will be how it is represented as a _string_.
19-
Other programmers who use or call Clock `objects` created from the Clock `class` will refer to this string representation for debugging and other activities.
20-
However, the default string representation on a `class` is usually not very helpful.
26+
Other programmers who use or call Clock `objects` created from the Clock `class` will refer to this string representation for debugging and other activities.
27+
However, the default representation on a custom `class` is not very helpful:
28+
2129

2230
```python
2331
>>> Clock(12, 34)
2432
<Clock object st 0x102807b20 >
2533
```
2634

27-
To create a more helpful representation, you can define a `__repr__` method on the `class` that returns a string.
35+
To create a more helpful representation, you can define a [`__repr__`][repr-method] [special method][dunder-methods] on the `class`.
2836

29-
Ideally, the `__repr__` method returns valid Python code that can be used to recreate the object -- as laid out in the [specification for a `__repr__` method][repr-docs].
30-
Returning valid Python code allows you to copy-paste the `str` directly into code or the REPL.
37+
Ideally, that `__repr__` method returns valid Python code that can be used to recreate the object when passed to [`eval()`][eval-built-in] — as laid out in the [specification for a `__repr__` method][repr-docs].
38+
Returning valid Python code allows another developer to copy-paste the `str` directly into code or the REPL.
39+
A `Clock` that represents 11:30 AM could look like this:
3140

32-
For example, a `Clock` that represents 11:30 AM could look like this: `Clock(11, 30)`.
41+
```python
42+
`Clock(11, 30)`
43+
```
3344

34-
Defining a `__repr__` method is good practice for all classes.
35-
Some things to consider:
45+
Defining a `__repr__` method is good practice for all custom classes.
46+
Some additional things to consider:
3647

3748
- The information returned from this method should be beneficial when debugging issues.
38-
- _Ideally_, the method returns a string that is valid Python code -- although that might not always be possible.
49+
- _Ideally_, the method returns a string that is valid Python code although that might not always be possible.
3950
- If valid Python code is not practical, returning a description between angle brackets: `< ...a practical description... >` is the convention.
4051

4152

4253
### String conversion
4354

4455
In addition to the `__repr__` method, there might also be a need for an alternative "human-readable" string representation of the `class`.
4556
This might be used to format the object for program output or documentation.
57+
This is done by writing a [`__str__`][str-dunder] special method.
4658
Looking at `datetime.datetime` again:
4759

60+
4861
```python
4962
>>> str(datetime.datetime(2022, 5, 4))
5063
'2022-05-04 00:00:00'
5164
```
5265

5366
When a `datetime` object is asked to convert itself to a string representation, it returns a `str` formatted according to the [ISO 8601 standard][ISO 8601], which can be parsed by most datetime libraries into a human-readable date and time.
5467

55-
In this exercise, you will get a chance to write a `__str__` method, as well as a `__repr__`.
68+
In this exercise, you will get a chance to write a `__str__` method for your Clock, as well as a `__repr__` method.
5669

5770
```python
5871
>>> str(Clock(11, 30))
5972
'11:30'
6073
```
6174

62-
To support this string conversion, you will need to create a `__str__` method on your class that returns a more "human readable" string showing the Clock time.
75+
To support this string conversion, you will need to create a `__str__` special method on your `class` that returns a more "human-readable" string showing the Clock time.
6376

64-
If you don't create a `__str__` method and you call `str()` on your class, python will try calling `__repr__` on your class as a fallback. So if you only implement one of the two, it would be better to create a `__repr__` method.
77+
If you don't create a `__str__` method and you call `str()` on your class, Python will try calling `__repr__` on your class as a fallback.
78+
So if you only implement one of these two special methods, it would be better to create a `__repr__` rather than just a `__str__`.
6579

66-
[repr-docs]: https://docs.python.org/3/reference/datamodel.html#object.__repr__
67-
[classes in python]: https://docs.python.org/3/tutorial/classes.html
68-
[REPL]: https://pythonprogramminglanguage.com/repl/
69-
[ISO 8601]: https://www.iso.org/iso-8601-date-and-time-format.html
7080

81+
[ISO 8601]: https://www.iso.org/iso-8601-date-and-time-format.html
82+
[REPL]: https://pythonprogramminglanguage.com/repl/
83+
[classes in python]: https://docs.python.org/3/tutorial/classes.html
84+
[datetime]: https://docs.python.org/3/library/datetime.html#available-types
85+
[dunder-methods]: https://www.pythonmorsels.com/every-dunder-method/
86+
[eval-built-in]: https://docs.python.org/3/library/functions.html#eval
87+
[repr-docs]: https://docs.python.org/3/reference/datamodel.html#object.__repr__
88+
[repr-method]: https://docs.python.org/3/library/functions.html#repr
89+
[str-dunder]: https://docs.python.org/3/reference/datamodel.html#object.__str__
90+
[str-rep-classes]: https://www.digitalocean.com/community/tutorials/python-str-repr-functions#introduction
91+
[what-is-an-object]: https://realpython.com/ref/glossary/object/
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
To keep things simple (and to let you focus on the important part of this exercise), your function will receive its inputs as binary strings:
1+
# Instructions Append
22

3-
```
3+
4+
To keep things simple (_and to let you focus on the important part of this exercise_), the tests will supply your function with _binary strings_ as arguments:
5+
6+
```python
47
>>> commands("00011")
58
["wink", "double blink"]
69
```

0 commit comments

Comments
 (0)