<jupyter_start><jupyter_text>**Constructors in Python**

- In this tutorial, we will learn about **Constructors in Python**.  **1. Constructors in Python**

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

- Of one particular interest is the `__init__()` function. This special function gets called whenever a new object of that class is instantiated.

- This type of function is also called **Constructors in Object Oriented Programming (OOP)**. We normally use it to initialize all the variables.<jupyter_code>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)<jupyter_output>2+3j
(5, 0, 10)
{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.02348178137651822, 'mean_word_length': 3.9243027888446216, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '564242', 'license': 'CC0-1.0', 'n_tokens_mistral': 400, 'n_tokens_neox': 381, 'n_words': 142, 'path': 'Constructors_in_Python.ipynb', 'repo_name': 'pb111/Python', 'size': '1235'}