The need for custom classes
Custom classes become invaluable when we need to organize and manage complex data structures efficiently. Let's illustrate this with an example involving the Advanced Programming course (AdvProg
) members. Initially, we store information in a dictionary:
advprog_1 = {'first': Pasquale', 'last': 'Africa', 'email': 'pafrica@sissa.it'}
To extract a member's full name, we define a function:
def full_name(first, last):
return f"{first} {last}"
This approach requires repetitive code for each member:
advprog_2 = {'first': 'Giuseppe', 'last': 'D\'Inverno', 'email': gdinvern@sissa.it'}
full_name(advprog_2['first'], advprog_2['last'])