Skip to content

Instantly share code, notes, and snippets.

@gorbiz
Created December 8, 2023 18:40
Show Gist options
  • Save gorbiz/f783a7183bc7410b827d6ed8afff518b to your computer and use it in GitHub Desktop.
Save gorbiz/f783a7183bc7410b827d6ed8afff518b to your computer and use it in GitHub Desktop.
Realistic example of local variable in python constructor
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