将字典值作为构造函数的参数传递

时间:2014-08-05 08:35:15

标签: python python-2.7

我是Python的新手。我需要创建一个简单的学生课程,其中包括名字,姓氏,身份证和将课程名称映射到其成绩的字典。

class Student:
    def __init__(self, firstName, lastName, id, _____ (dictionary values)):
        self._firstName = firstName;
        self._lastName = lastName;
        self._id = id;

        self.

我的问题是如何在构造函数中初始化字典值?

例如,假设我想在等级映射中添加3门课程: “数学:100” “生物:90” “历史:80”

例如:

student1 = Student("Edward", "Gates", "0456789", math: 100, bio: 90, history: 80)

最后3个值应该进入字典。

由于可以作为字典一部分的键值的数量可以变化,我应该在构造函数参数签名中写什么?

我想在调用构造函数时发送所有学生值...

5 个答案:

答案 0 :(得分:7)

如果你想在python中添加一个字典Mathias'的答案就足够了key word arguments

但是,如果您希望从关键字参数添加对象变量,则需要setattr

例如,如果你想要这样的东西:

student1 = Student("Edward", "Gates", "0456789", {'math': 100, 'bio': 90, 'history': 80})
print student1.math #prints 100
print student1.bio  #prints 90

然后这将解决问题:

class Student(object):
    def __init__(self, first_name, last_name, id, **kwargs):
        self.first_name = first_name
        self.last_name = last_name
        self.id = id
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

student1 = Student("Edward", "Gates", "0456789", {'math': 100, 'bio': 90, 'history': 80})

请注意, ** kwargs 只会解压缩字典或元组元组之类的内容。如果您希望发送没有键的值列表,则应使用 * args 。请查看here了解详情。

答案 1 :(得分:2)

Python为您收集所有关键字参数。

class Student:
    def __init__(self, firstName, lastName, id, **kwargs):
        self._firstName = firstName;
        self._lastName = lastName;
        self._id = id;

        self. _grades = kwargs

Here is an excellent explanation about kwargs in python

答案 2 :(得分:1)

您可以尝试以下内容:

student = Student("Edward", "Gates", "0456789", {"math": 100, "bio": 90, "history": 80})

在构造函数中,您可以将这些值复制到新字典中:

class Student:
    def __init__(self, firstName, lastName, id, grades):
        self._firstName = firstName;
        self._lastName = lastName;
        self._id = id;

        self._grades = grades.copy()

请注意,我们正在将字典复制到新属性,因为我们希望避免保留引用。

答案 3 :(得分:1)

为什么不将完整的成绩字典发送到您的班级并将其存储在变量中。 (另请注意,在Python中,行末没有分号)

class Student:
    def __init__(self, firstName, lastName, id, grade_dict):
        self._firstName = firstName
        self._lastName = lastName
        self._id = id
        self._grades = grade_dict

    def get_grades(self):
        return self._grades

然后当你想初始化和使用成绩时:

student1 = Student("Edward", "Gates", "0456789", {'math': 100, 'bio': 90, 'history': 80})
grades = student1.get_grades()
for key, value in grades.items():
    print 'Marks in {}: {}'.format(key, str(value))

打印哪些:

Marks in bio: 90
Marks in math: 100
Marks in history: 80

答案 4 :(得分:0)

首先,确保从代码中删除分号; - 它不会编译! 其次,我相信你正在做类似的事情:

class Student:

    def __init__(self, first_name, last_name, _id, **courses):
        self._first_name = first_name
        self._last_name = last_name
        self._id = _id
        self.courses = courses

    def print_student(self):
        print self._first_name
        print self._last_name
        print self._id
        for key in self.courses:
            print key, self.courses[key]


courses = {'math': 100, 'bio': 90, 'history': 80}    
s = Student("John", "Smith", 5, **courses)
s.print_student()

<强>输出

John
Smith
5
bio 90
math 100
history 80
相关问题