Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from my_classes import MyAge

my_age = MyAge("1985-01-01", "Mr James")
my_age.show_me_my_age()
print(my_age.show_me_my_age())
# > 'Mr James, you are so young, only 41 years old!'

# additional learexample with the import as command
15 changes: 9 additions & 6 deletions src/my_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ def __init__(self, date_of_birth, my_name):
# create print function
def show_me_my_age(self):
return f"{self.__my_name}, you are so young, only {self.__my_age_years} years old!"


# instantiate the class and execute the print function
age = MyAge("1982-08-04", "Mr James")
print(age.show_me_my_age())
# > 'Mr James, you are so young, only 39 years old!'
# Fix for the issue shown in training video "Python for data engineers > Advanced Python > Modules":
# Without this guard, importing this file from modules.py would also execute the block below,
# printing the hardcoded date instead of the date passed by the caller.
# if __name__ == "__main__" ensures this block only runs when this file is executed directly.
if __name__ == "__main__":
# instantiate the class and execute the print function
age = MyAge("1982-08-04", "Mr James")
print(age.show_me_my_age())
# > 'Mr James, you are so young, only 46 years old!'