You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
5
11
## Representing your class
6
12
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
+
9
16
10
17
```python
11
18
>>>from datetime import datetime
@@ -14,57 +21,71 @@ For example, if you were to create a new `datetime.datetime` object in the Pytho
14
21
datetime.datetime(2022, 5, 4, 0, 0)
15
22
```
16
23
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.
18
25
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
+
21
29
22
30
```python
23
31
>>> Clock(12, 34)
24
32
<Clock object st 0x102807b20>
25
33
```
26
34
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`.
28
36
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:
31
40
32
-
For example, a `Clock` that represents 11:30 AM could look like this: `Clock(11, 30)`.
41
+
```python
42
+
`Clock(11, 30)`
43
+
```
33
44
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:
36
47
37
48
- 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.
39
50
- If valid Python code is not practical, returning a description between angle brackets: `< ...a practical description... >` is the convention.
40
51
41
52
42
53
### String conversion
43
54
44
55
In addition to the `__repr__` method, there might also be a need for an alternative "human-readable" string representation of the `class`.
45
56
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.
46
58
Looking at `datetime.datetime` again:
47
59
60
+
48
61
```python
49
62
>>>str(datetime.datetime(2022, 5, 4))
50
63
'2022-05-04 00:00:00'
51
64
```
52
65
53
66
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.
54
67
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.
56
69
57
70
```python
58
71
>>>str(Clock(11, 30))
59
72
'11:30'
60
73
```
61
74
62
-
To support this string conversion, you will need to create a `__str__` method on your class that returns a more "humanreadable" 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.
63
76
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__`.
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
2
2
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:
0 commit comments