init in Python
In this post, you will learn how to work with init so that you can use that same knowledge and thus evolve.
- init is basically a constructor and its function is to initialize everything that is necessary when creating a certain object.
class Person:
def __init__(self, firstName, lastName, age):
self.firstName = firstName
self.lastName = lastName
self.age = age
def information(self):
print('Name:', self.firstName, self.lastName)
print('Age:', self.age)
def checkEntrance(self):
if self.age >= 18:
print('This person can enter the place because he is of legal age.')
else:
print('This person cannot enter the site because he is a minor.')
pessoa1 = Person('Nelson', 'Silva', 25)
pessoa2 = Person('Larissa', 'Fernandes', 17)
pessoa1.information()
pessoa1.checkEntrance()
'''
Name: Nelson Silva
Age: 25
This person can enter the place because he is of legal age.
'''
pessoa2.information()
pessoa2.checkEntrance()
'''
Name: Larissa Fernandes
Age: 17
This person cannot enter the site because he is a minor.
'''
Don’t forget to watch the video and you can always read this post in Portuguese.