Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Classes and objects/Special __init__ method/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ The `__init__()` method may receive arguments for greater flexibility.
In that case, arguments given to the class instantiation operator are passed
on to `__init__()`. For example:
```python
class Complex:
class ComplexNumber:
def __init__(self, real_part, imag_part):
self.r = real_part
self.i = imag_part
self.num = complex(self.r, self.i)

x = Complex(3.0, -4.5) # Instantiating a complex number
x = ComplexNumber(3.0, -4.5) # Instantiating a complex number
x.num
```
```text
Expand Down
8 changes: 4 additions & 4 deletions Classes and objects/The self parameter/self_parameter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Complex:
def create(self, real_part, imag_part):
class ComplexBuilder:
def set_parts(self, real_part, imag_part):
self.r = real_part
self.i = imag_part

def build(self):
self.num = complex(self.r, self.i)


complex_number = Complex() # Instantiate a complex number object
complex_number.create(12, 5) # Call create method with real_part = 12 and imag_part = 5
complex_number = ComplexBuilder() # Instantiate a complex number object
complex_number.set_parts(12, 5) # Call set_parts method with real_part = 12 and imag_part = 5
complex_number.build() # Build the complex number
print(complex_number.num)

Expand Down
4 changes: 2 additions & 2 deletions Classes and objects/The self parameter/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ files:
- name: self_parameter.py
visible: true
placeholders:
- offset: 484
- offset: 507
length: 22
placeholder_text: "# Update the current attribute by adding the amount to it."
- offset: 550
- offset: 573
length: 12
placeholder_text: ???
- name: tests/__init__.py
Expand Down
6 changes: 3 additions & 3 deletions Classes and objects/__str__ vs __repr__/str_and_repr.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Complex:
class ComplexNumber:
def __init__(self, real_part, imag_part):
self.real = real_part
self.img = imag_part

def __repr__(self):
return f'Complex({self.real}, {self.img})'
return f'ComplexNumber({self.real}, {self.img})'

def __str__(self):
return f'{self.real} + i{self.img}'


x = Complex(2, 5)
x = ComplexNumber(2, 5)
print(str(x))
print(repr(x))

Expand Down
4 changes: 2 additions & 2 deletions Classes and objects/__str__ vs __repr__/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ files:
- name: str_and_repr.py
visible: true
placeholders:
- offset: 455
- offset: 473
length: 46
placeholder_text: ???
- offset: 541
- offset: 559
length: 45
placeholder_text: ???
- name: tests/__init__.py
Expand Down
2 changes: 1 addition & 1 deletion Classes and objects/__str__ vs __repr__/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defined in the object's class.

Our own defined class should therefore have a `__repr__` if we need detailed information for debugging.
Also, if we think it would be useful to have a string representation for users, we should create
a `__str__` function. Check out another implementation of the class `Complex` in the code editor. Run the code
a `__str__` function. Check out another implementation of the class `ComplexNumber` in the code editor. Run the code
to see what each of the two `print` statements prints.

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/7139#str__-vs-__repr?utm_source=jba&utm_medium=jba_courses_links).
Expand Down