在使用 .append 或 .insert 时,类中出现重复项 (Python)。

huangapple go评论49阅读模式
英文:

Duplicated items in classes when use .append or .insert (Python)

问题

我正在学习Python,通常也会执行一些非常简单的个人任务,以帮助我记住这门新语言。问题是,我遇到了一个我不太清楚出了什么问题的情况,也许有人可以解释一下。我在这方面是个新手,所以对于你来说可能很容易看出我的问题,但我已经费了好一阵子的脑筋,但我不明白问题出在哪里。

问题是,当我使用.insert.append时,我在终端上收到了列表中的重复值。

这个代码相当简单:

class Student:
    def __init__(self, name, surname, age):
        Student.name = name
        Student.surname = surname
        Student.age = age
        Student.subjects = [] # 可选属性,作为列表。

student001 = Student("Mary", "Stone", 17)
student002 = Student("James", "Lincoln", 16)

student001.subjects.append("English")
student002.subjects.append("English")

print(student001.subjects)
print(student002.subjects)

student001.subjects.append("P.E.")
student002.subjects.insert(1, "P.E.")

print(student001.subjects)
print(student002.subjects)

问题出在当我打印它时,终端上会收到重复的值:

['English', 'English']
['English', 'English']
['English', 'P.E.', 'English', 'P.E.']
['English', 'P.E.', 'English', 'P.E.']

有人可以解释一下我做错了什么吗?

提前谢谢! 在使用 .append 或 .insert 时,类中出现重复项 (Python)。

我想收到这个输出:

['English']
['English']
['English', 'P.E.']
['English', 'P.E.']
英文:

I am learning Python and usually do really easy personal tasks too keep in my mind all this new language. The thing is, that I am having an issue that I don't really know what is wrong and maybe soomeone can explain. I am a noob in all this, so maybe for sou it is so easy to see my problem, but I've been breaking my brain for a while and I cannot understand what is wrong.

The thing is that I am receiving duplicated values on the terminal from a list when I .instert or .append them.

The code it's quite simple:

class Student:
    def __init__(self, name, surname, age):
        Student.name = name
        Student.surname = surname
        Student.age = age
        Student.subjects = [] # Atributo no obligatorio en forma de LIST.

student001 = Student("Mary", "Stone", 17)
student002 = Student("James", "Lincoln", 16)


student001.subjects.append("English")
student002.subjects.append("English")

print(student001.subjects)
print(student002.subjects)

student001.subjects.append("P.E.")
student002.subjects.insert(1, "P.E.")

print(student001.subjects)
print(student002.subjects)

The problem is when I print it and I receive duplicated values on the terminal:

['English', 'English']
['English', 'English']
['English', 'P.E.', 'English', 'P.E.']
['English', 'P.E.', 'English', 'P.E.']

May anyone explain me what I am doing wrong?

Thanks in advence! 在使用 .append 或 .insert 时,类中出现重复项 (Python)。

I want to receive this:

['English']
['English']
['English', 'P.E.']
['English', 'P.E.']

答案1

得分: 0

你正在定义属性(所有实例共享),而不是实例属性,每个实例都是独特的。用self替换Student

class Student:
    def __init__(self, name, surname, age):
        self.name = name
        self.surname = surname
        self.age = age
        self.subjects = [] # Atributo no obligatorio en forma de LIST.
英文:

You are defining class attributes (shared by all instances) rather than instance attributes unique to each instance. Replace Student with self:

class Student:
    def __init__(self, name, surname, age):
        self.name = name
        self.surname = surname
        self.age = age
        self.subjects = [] # Atributo no obligatorio en forma de LIST.

huangapple
  • 本文由 发表于 2023年2月14日 01:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定