繁体   English   中英

列表中的 Python class 属性

[英]Python class attributes from list

我有一段代码用于创建 class、 self.first_nameself.last_nameself.email的 3 个属性:

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

并且基本上只是注意到我在重复自己,并且可能可以使用 for 循环和属性列表更有效地做到这一点。 所以我尝试了这个:

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

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

这并没有达到预期的效果,我认为这里的每个属性都是self.attr而不是预期的名称。 任何人都可以提出解决此问题的方法吗? 我敢肯定,一定有比每次都写出来更好的方法。

可以肯定的是,可能有多种方法可以为 class 提供不同数量的属性和值对。 以下是在初始化具有不同信息量的人员类别时使用列表的简单原理证明方法。

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])

在这种可能的情况下,属性和值在属性/值对中初始化,这与字典不同。 此特定场景确实具有必需的属性,例如名字和姓氏。 然后,可以为创建的每个人包含可选属性。

以下是 output 的样本,用于两个具有不同数量属性的人的条目。

@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

如前所述,这只是创建具有不同属性的各种 class 对象的一种可能方法。 试试看。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM