Created
December 8, 2023 18:40
-
-
Save gorbiz/f783a7183bc7410b827d6ed8afff518b to your computer and use it in GitHub Desktop.
Realistic example of local variable in python constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person: | |
def __init__(self, full_name): | |
# 'name_parts' is a local variable, only used within the constructor | |
name_parts = full_name.split() | |
self.first_name = name_parts[0] | |
self.last_name = name_parts[-1] if len(name_parts) > 1 else '' | |
def display_name(self): | |
print(f"First Name: {self.first_name}, Last Name: {self.last_name}") | |
# Example usage | |
person = Person("John Doe") | |
person.display_name() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment