-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathtest_class_objects.py
More file actions
62 lines (44 loc) · 1.95 KB
/
test_class_objects.py
File metadata and controls
62 lines (44 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Class Definition Syntax.
@see: https://docs.python.org/3/tutorial/classes.html#class-objects
After defining the class attributes to a class, the class object can be created by assigning the
object to a variable. The created object would have instance attributes associated with it.
"""
def test_class_objects():
"""Class Objects.
Class objects support two kinds of operations:
- attribute references
- instantiation.
"""
class ComplexNumber:
"""Example of a complex number class"""
def __init__(self, real=0, imaginary=0):
"""Initialize complex number with default values."""
self.real = real
self.imaginary = imaginary
def get_real(self):
"""Return real part of complex number."""
return self.real
def get_imaginary(self):
"""Return imaginary part of complex number."""
return self.imaginary
assert ComplexNumber(0, 0).real == 0
# __doc__ is also a valid attribute, returning the docstring belonging to the class
assert ComplexNumber.__doc__ == "Example of a complex number class"
# Class attributes can be changed dynamically
ComplexNumber.real = 10
assert ComplexNumber.real == 10
# Creating an instance
complex_number = ComplexNumber(10, -5)
assert complex_number.real == 10
assert complex_number.get_real() == 10
# Reset real value to default
ComplexNumber.real = 0
assert ComplexNumber.real == 0
# Using constructor-based initialization
class ComplexNumberWithConstructor(ComplexNumber):
"""Example of a complex number class with constructor"""
def __init__(self, real, imaginary):
super().__init__(real, imaginary) # Use super() to inherit from ComplexNumber
complex_number = ComplexNumberWithConstructor(3.0, -4.5)
# ✅ Fixed the assertion syntax here
assert (complex_number.real, complex_number.imaginary) == (3.0, -4.5)