Home Python Object & Class 2
Post
Cancel

Python Object & Class 2

Constructors In Python

Class functions that begin with double underscore __ are called special functions as they have special meaning.

The __init__() method is of great significance. Every time a new object of that class is created, this specific function is called. In object-oriented programming, this kind of function is also known as a constructor (OOP). Typically, we utilize it to initialize each variable.

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
class ComplexNumber:
    def __init__(self, r=0, i=0):
        self.real = r
        self.imag = i

    def get_data(self):
        print(f'{self.real}+{self.imag}j')

# Create a new ComplexNumber object
num1 = ComplexNumber(2, 3)

# Call get_data() method
# Output: 2+3j
num1.get_data()

# Create another ComplexNumber object
# and create a new attribute 'attr'
num2 = ComplexNumber(5)
num2.attr = 10

# Output: (5, 0, 10)
print((num2.real, num2.imag, num2.attr))

# but c1 object doesn't have attribute 'attr'
# AttributeError: 'ComplexNumber' object has no attribute 'attr'
print(num1.attr)

Output:
2+3j
(5,0,10)
Traceback (most recent call last):
    File "<string>", line 27, in <module>
        print(num1.after)
AttributeError: 'ComplexNumber' object has no attribute 'attr'

We created a new class to represent complex numbers in the example above. It has two functions: get_data() to appropriately show the number and __init__() to initialize the variables (defaults to zero). In the step above, it’s noteworthy to observe that attributes for an object can be produced instantly. For object num2, we added a new attribute called attr and read it as well. But this does not provide object num1 that attribute.

Deleting Attributes and Objects

Using the del statement, any attribute of an object may be erased whenever desired. To see the results, run the following command in the Python shell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
num1 = ComplexNumber(2,3)
del num1.imag
num1.get_data()

Output:
    ...
    AttributeError: 'ComplexNumber' object has no attribute 'imag'

del ComplexNumber.get_data
num1.get_data()

Output:
    ...
    Traceback (most recent call last):
    ...
    AttributeError: 'ComplexNumber' object has no attribute 'get_data'

We can even delete the object itself, using the del statemtn.

1
2
3
4
5
6
7
8
c1 = ComplexNumber(1,3)
del c1
c1

Output:
    Traceback (most recent call last):
    ...
    NameError: name `c1` is not defined

Actually, it is more complicated than that. When we do c1 = ComplexNumber(1,3), a new instance object is created in memory and the name c1 binds with it.

On the command del c1, this binding is removed and the name c1 is deleted from the corresponding namespace. The object however continues to exist in memory and if no other name is bound to it, it is later automatically destroyed.

This automatic destruction of unreferenced objects in Python is also called garbage collection.

img

This post is licensed under CC BY 4.0 by the author.