0

I am learning OOP in Python. I created a class and tried to update an instance variable inside a method, but the value doesn’t change.

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

    def update_marks(self, value):
        marks = value   # not updating self.marks ?

s = Student("Shivam", 80)
s.update_marks(95)
print(s.marks)  # still prints 80
New contributor
Shivam Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • 4
    marks = value # not updating self.marks ? Yes, that's correct. What are you confused about? Did you just forget to type self., or were you expecting marks = to be equivalent to self.marks =? That's a perfectly reasonable assumption coming from other programming languages, it's just not how Python works. Please edit to clarify. Commented 13 hours ago
  • @wjandrea If you tell the OP that they need self.marks = value and you explain why, does it really matter if they forgot the self. or thought it was unnecessary? They will now know the complete story, so what is gained by making them edit the question just to confess their forgetfulness or ignorance? And whoever voted to close this question because it was either a typo or non-reproducible is doing the OP a disservice because it was a legitimate minimal reproducible example and only maybe was it a typo. I think the OP deserves an answer, which closing prevents. Commented 13 hours ago
  • @wjandrea I also think from the comment # not updating self.marks? it is pretty clear that they thought the self. was unnecessary. Commented 13 hours ago
  • 2
    @Booboo When I'm thinking about an answer, I use Occam's Razor: the simplest explanation is probably correct, and the simplest answer here is that they just forgot. (I've made plenty of dumb mistakes myself when sleep-deprived, say.) But if there's actually a rationale behind the question, then there's an interesting answer. Note that they used self.marks = in __init__, so they know that that's the correct way to do it in at least some contexts, but it's not clear why they thought different methods would allow different syntax. Commented 13 hours ago
  • 1
    @booboo And if the latter, this may be a duplicate question of: What is the purpose of the self parameter? Why is it needed? The top answer is a little too brief, but Thomas Wouters's answer is better. Commented 13 hours ago

2 Answers 2

1

When you execute marks = value, you are only creating a variable that is local to your update_marks method; you are not updating the instance's marks attribute, which is accessed via the method's self argument:

    ...

    def update_marks(self, value):
        self.marks = value

When you execute s.update_marks(95), s is the instance you are trying to update and will be passed to update_marks as the self argumnent.

Sign up to request clarification or add additional context in comments.

Comments

0

Python does not implicitly treat names as instance variables inside a method. Just like in __init__, you need to explicitly assign to the attribute self.marks, as the class itself has no notion of which attributes exist.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.