'How can the __init__ method accept objects and assign attributes if the attributes have not been created in the Constructor? [duplicate]

I am new to OOP in Python.

As the title suggests, I am unsure as to how the __init__ method can assign attributes to an object of a given class if the attributes have not been defined in the constructor.

I tried some experimenting around with the following code:

class initPlane:
    def __init__(self, manufacturer)
        print(manufacturer)
    def get_manufacturer(self):
        return self.manufacturer

cessna = initPlane("Cessna")
print(cessna.get_manufacturer())

Obviously I received the "AttributeError" that the class initPlane has no attribute manufacturer when the method 'get_manufacturer' tries to receive the manufacturer attribute of the instance cessna.

To fix this it is clear that I need to create the manufacturer attribute by altering the code of the constructor as follows:

def __init__(self, manufacturer)
        self.manu = manufacturer
        print(manufacturer)

What is confusing... is that I do not understand how I am able to print the manufacturer attribute, without creating the attribute in the first place. The following code returns a result without errors.

class initPlane:
    def __init__(self, manufacturer)
        print(manufacturer)
cessna = initPlane("Cessna")

I do not know how the method is able to print the manufacturer, without me first creating the attributes within the constructor.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source