如何将函数结果作为类的参数?

2024-04-29 07:16:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,将询问有关参数的用户,并将其转移到类,但我找不到如何。我试着把这个函数作为参数,但没有成功

class Exam:
    def __init__(self, day, importance):
        self.day = day
    self.importance = importance


days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
]


def is_important():
    for day in days:
        answer = input("Ìs " + day + " Important")
        if answer == "True":
            return True
        elif answer == "False":
            return False


is_important() 

mon = Exam("Monday",)
tue = Exam("Tuesday", )
wed = Exam("Wednesday", )
thu = Exam("Thursday", )
fri = Exam("Friday", )
sat = Exam("Saturday", )
sun = Exam("Sunday", )

print(mon.importance)
print(tue.importance)
print(wed.importance)
print(thu.importance)
print(fri.importance)
print(sat.importance)
print(sun.importance)

Tags: answerself参数defdaysprintexammonday
3条回答

实现这一点的最佳方法是使一个is\u成为一个重要的类方法。这是一个更为python和明确之间的联系问题和工作日。从本质上讲,它更面向对象,因为它的重要性和要求都与考试对象有明显的联系。事情是这样的:

class Exam:
    def __init__(self, day):
        self.day = day
        self.is_important()

    def is_important(self):
        answer = input("Ìs " + self.day + " Important? ")
        if answer == "Yes":
            self.importance = True
        elif answer == "No":
            self.importance = False

然后在循环中初始化每天的检查对象,并将结果添加到字典中。字典的优点是,您可以初始化多个变量,不需要键入额外的行,这样它们就有一个清晰的变量名。如果您有许多相同类型的对象,那么字典的键是比普通变量名更好的选择


days = ["Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday"]

my_week = {}

for day in days:
    my_week.update({day: Exam(day)})
    print my_week[day].importance

is_important()应将日期作为参数。然后你可以用不同的日期调用它,并将结果传递给全班

def is_important(day):
    answer = input("Ìs " + day + " Important")
    return answer.lower() == "true"

mon = Exam("Monday", is_important("Monday");
tue = Exam("Tueday", is_important("Tuesday");
...

最好使用字典,而不是每天使用单独的变量。然后你可以做:

exam_days = {day: Exam(day, is_important(day)) for day in days}

在考虑问题时,这可能是您想要考虑的一种方法,如果您想将改变重要性的能力与考试对象结合起来:

class Exam:

    # COMMENT: set a default importance of False...
    def __init__(self, day, importance=False):
        self.day = day
        self.importance = importance

    # COMMENT: since you provide the day when the object is created,
    #     use that value (ie, self.day) in the input function call
    #     and overwrite the existing self.importance value.
    def is_important(self):
        answer = input("Ìs " + self.day + " Important?")
        if answer == "True":
            self.importance = True
        elif answer == "False":
            self.importance = False

# COMMENT: When you call the Class, you can provide it with the day 
#     and immediately provide it with an importance level.

mon = Exam("Monday", True)
tue = Exam("Tuesday", False)

# COMMENT: but you also have the flexibility to call the class and accept
#     the default importance OR overwrite it later by calling the 
#     is_important() method
wed = Exam("Wednesday")
wed.is_important()

print(mon.importance)
print(tue.importance)
print(wed.importance)

相关问题 更多 >