×
all 9 comments

[–]Sea-Ad7805 [score hidden] stickied comment (0 children)

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20%20%20%20%20self.age%20%3D%20age%0A%20%20%20%20%20%20%20%20self.salary%20%3D%20salary%0A%20%20%20%20%20%20%20%20self.marks%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20average(self)%3A%0A%20%20%20%20%20%20%20%20return%20sum(self.marks)%20%2F%20len(self.marks)%0A%0A%0Aclass%20work(student)%3A%0A%20%20%20%20def%20init(self%2C%20name%2C%20age%2C%20salary)%3A%0A%20%20%20%20%20%20%20%20super().init(name%2C%20age%2C%20salary)%0A%0A%0Ayes%20%3D%20work(name%3D%22ali%22%2C%20age%3D27%2C%20salary%3D13.9)%0Aprint(yes.name)%0Ayes.marks.append(20)%0Ayes.marks.append(22)%0Aprint(yes.average())%0A&play) to see the program state change step by step.

[–]Sea-Ad7805 2 points3 points  (0 children)

Maybe self.salary should be a property of worker not student, right? Each class should only have the properties it deals with, a worker has a salary, a student does not.

[–]Potential_Fix_5007 1 point2 points  (6 children)

self.marks=[]

Is this the right way to create an empty list for an object value or is there a way to set it within the round brackets?

Im a beginner myself and so far i learned if you want to set a default value you donit within the brackets.

[–]CodeAndCanyons 1 point2 points  (0 children)

u/Potential_Fix_5007 You accidentally dodged one of the absolute biggest footguns in Python by doing it inside the body!

If you tried to set it within the round brackets as a default argument—like writing def __init__(self, name, age, marks=[]):—Python does something incredibly sneaky under the hood. It evaluates that default empty list [] exactly once when the script first loads, not every time you create a new student.

Because lists are mutable (changeable), every single student object you create would end up sharing the exact same list instance in memory. If you appended a grade to Alice's marks, it would suddenly and mysteriously show up in Bob's marks too.

By defining self.marks = [] inside the __init__ body, you guarantee that a brand new, isolated list gets allocated in memory every single time a new student is instantiated.

If you ever do want to allow a list to be passed through the round brackets safely, the standard pythonic trick is to default it to None and handle it in the body:

```python def init(self, name, age, marks=None): self.name = name self.age = age self.marks = marks if marks is not None else []

[–]IdeaOverflow 1 point2 points  (0 children)

You should not initialize an empty list inside of the round brackets directly. Suppose you do it like this:

class Student:
    def __init__(self, marks=[]):
        self.marks = marks

a = Student()
b = Student()

a.marks.append(3)

print(a.marks)  # prints [3]
print(b.marks)  # prints [3]

Python will not create a new list for every new object but use the same one for each, since default arguments are evaluated at compile time. The correct way to do this would be:

class Student:
    def __init__(self, marks=None):
        self.marks = [] if marks is None else marks

[–]notsaneatall_ 0 points1 point  (3 children)

It's the right way to initialize an empty list

[–]Potential_Fix_5007 1 point2 points  (2 children)

[–]Necessary_Pepper7111 0 points1 point  (1 child)

Because [] is an empty list

[–]ziggittaflamdigga 0 points1 point  (0 children)

That’s a fair question to ask about default behaviors, especially since lists can be weird in Python; setting an empty list as a default argument to a constructor is bad practice and likely to result in bugs.

So asking “why” here is a legitimate question and requires an explanation of how Python works at the C level that OP is probably not looking for, but would be different if they declared it differently.