Now that you've begun to see OOP and class structures, it's time to investigate the init method more. The init method allows classes to have default behaviors and attributes.
You will be able to:
- Define custom init methods for object initialization
- Create instance variables in the init method
- Use default arguments in the init method
By using the __init__
method, you can initialize instances of objects with defined attributes. Without this, attributes are not defined until other methods are called to populate these fields, or you set attributes manually. This can be problematic. For example, if you had tried to call the greet_passeneger
method from the previous lab without first setting the driver's first and last attributes, you would have encoutnered an error. Here's another example to demonstrate:
class Person:
def set_name(self, name):
self.name = name
def set_job(self, job):
self.job = job
bob = Person()
If we try to access an attribute before setting it we'll get an error.
bob.name
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-b123a67a06c2> in <module>()
----> 1 bob.name
AttributeError: 'Person' object has no attribute 'name'
bob.set_name('Bob')
bob.name
'Bob'
To avoid errors such as this, you can use the init method to set attributes on instantiation.
class Person:
def __init__(self, name, job):
self.name = name
self.job = job
bob = Person('Bob', 'Carpenter')
print(bob.name)
print(bob.job)
Bob
Carpenter
Written like this, these arguments then become required:
someone = Person()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-1ac56b0c183e> in <module>()
----> 1 someone = Person()
TypeError: __init__() missing 2 required positional arguments: 'name' and 'job'
To circumvent this, we can also define init to have default arguments. This allows parameters to be specified if desired but are not required.
class Person:
def __init__(self, name=None, job=None):
self.name = name
self.job = job
someone = Person()
print(someone.name)
print(someone.job)
print('\n')
governer = Person(job = 'Carpenter')
print(governer.name)
print(governer.job)
print('\n')
bob = Person('Bob', 'Carpenter')
print(bob.name)
print(bob.job)
None
None
None
Carpenter
Bob
Carpenter
In this lesson you got a brief introduction to the init method and how you can use it to set attributes when objects are initialized, including default parameters.