Constructors, Attributes & Objects

Like function definitions begin with the keyword def, in Python, we define a class using the keyword class.

The first string is called docstring and has a brief description about the class. Although not mandatory, this is recommended.

There are also special attributes in it that begins with double underscores (). For example, doc__ gives us the docstring of that class.

class MyClass:
    "This is my second class"
    a = 10
    def func(self):
        print('Hello')
        
# Output: 10
print(MyClass.a)

# Output: <function MyClass.func at 0x0000000003079BF8>
print(MyClass.func)

# Output: 'This is my second class'
print(MyClass.__doc__)

10

<function MyClass.func at 0x000001FE651F8948>

This is my second class

Defining a new Object:

Constructors in Python:

2+3j

(5,0,10)

AttributeError Traceback (most recent call last)

in 24 # but c1 object doesn't have attribute 'attr' 25 # AttributeError: 'ComplexNumber' object has no attribute 'attr' ---> 26 c1.attr

AttributeError: 'ComplexNumber' object has no attribute 'attr'

Deleting Attributes & Objects:

Last updated

Was this helpful?