r/PythonLearning 4d ago

<python code>

Post image

This is python code to get student salary!

44 Upvotes

10 comments sorted by

View all comments

2

u/Potential_Fix_5007 4d ago

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.

1

u/notsaneatall_ 4d ago

It's the right way to initialize an empty list

2

u/Potential_Fix_5007 4d ago

1

u/Necessary_Pepper7111 4d ago

Because [] is an empty list

1

u/ziggittaflamdigga 4d ago

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.