Python class attributes from list

Viewed 55

I had a section of my code that worked to create 3 attributes of a class, self.first_name, self.last_name, and self.email:

    self.first_name = input("First name: ")
    self.last_name = input("Last name: ")
    self.email = input("Email: ")
    

And basically just noticed that I was repeating myself and could probably do this more efficiently using a for loop and a list of the attributes. So I tried this:

    attrs = ["first_name", "last_name", "email"]

    for attr in attrs:
        self.attr = input(f"{attr} :")

This doesn't quite have the desired effect, I think here every attribute is self.attr rather than the intended names. Can anyone suggest a way to fix this? I'm sure there must be a better way than writing it out every time.

1 Answers

To be sure, there could be a multitude of ways to have a varying number of attribute and value pairs for a class. Following is a simple proof-of-principle method using lists when initializing classes of people with varying amounts of information.

class Person():

    def __init__(self, attr_size, attrs, values):
        self.attr = []
        self.value = []
        self.size = attr_size
    
        for i in range(0, self.size):
            self.attr.append(attrs[i])
            self.value.append(values[i])
            
peep = []

while True:
    whoami = input("Do you want to enter in a person (Y/N) ")
    if whoami.upper() == "N":
        break
    x = 0
    att = []
    val = []
    
    att.append('First_Name')
    val.append(input("Enter person's first name: "))
    x += 1
    
    att.append('Last_Name')
    val.append(input("Enter person's last name: "))
    x += 1
    
    while True:
        more_values = input("Do you want to enter in more detail for this person (Y/N) ")
        if more_values.upper() == "N":
            break
            
        att.append(input("Enter additional attribute name "))
        val.append(input("Enter value for this attribute "))
        x += 1
    
    peep.append(Person(x, att, val))

for p in peep:
    for a in range (0, len(p.attr)):
        print(p.attr[a], p.value[a])

In this possible scenario, attributes and values are initialized in attribute/value pairs not unlike a dictionary. This particular scenario does have required attributes such as first and last name. Then, optional attributes can be included for each person created.

Following was a sample of output for the entry of two people with differing numbers of attributes.

@Una:~/Python_Programs/Person$ python3 Person.py 
Do you want to enter in a person (Y/N) Y
Enter person's first name: John
Enter person's last name: Doe
Do you want to enter in more detail for this person (Y/N) Y
Enter additional attribute name Age
Enter value for this attribute 66
Do you want to enter in more detail for this person (Y/N) N
Do you want to enter in a person (Y/N) Y
Enter person's first name: Jane
Enter person's last name: Doe
Do you want to enter in more detail for this person (Y/N) Y
Enter additional attribute name Age
Enter value for this attribute 64
Do you want to enter in more detail for this person (Y/N) Y
Enter additional attribute name Gender
Enter value for this attribute Female
Do you want to enter in more detail for this person (Y/N) Y
Enter additional attribute name Education
Enter value for this attribute BA
Do you want to enter in more detail for this person (Y/N) N
Do you want to enter in a person (Y/N) N
First_Name John
Last_Name Doe
Age 66
First_Name Jane
Last_Name Doe
Age 64
Gender Female
Education BA

As noted, this is just but one possible way to create various class objects with varying attributes. Give that a try.

Related