列表中的 Python 类属性

问题描述 投票:0回答:2

我的代码中有一部分用于创建类的 3 个属性:

self.first_name
self.last_name
self.email

    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
而不是预期的名称。谁能建议一种方法来解决这个问题?我确信肯定有比每次都写出来更好的方法。

python oop attributes
2个回答
0
投票

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

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

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

以下是具有不同属性数量的两个人的输入的输出示例。

@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

如上所述,这只是创建具有不同属性的各种类对象的一种可能方法。尝试一下。


0
投票

这应该有效:

ATTR_LIST = [
    'first_name', 
    'last_name', 
    'email'
]

class Foo():
    def __init__(self):
        for attr in ATTR_LIST:
            val = input(f"{attr} :")
            setattr(self, attr, val)
© www.soinside.com 2019 - 2024. All rights reserved.