-1

need help in understanding the variables in the below code.

class Student:
def __init__(self,m1,m2):
    self.m1 = m1
    self.m2 = m2
def __add__(self, other):
    m1 = self.m1 + self.m2 <---- doubt
    m2 = other.m2 + other.m2
    s3 = Student(m1,m2)
    return s3


s1 = Student(50,60)
s2 = Student(70,80)

s3 = s1 + s2
print (s3.m1)

So i get the desired output of 110 here. But my question is why should the variable in add function be declared only as m1 and not as any other variable. Does it have to match with the variable name in "init" function? If yes what is the reason for it.

Bhargav Mg
  • 337
  • 4
  • 12
  • I'm not sure I understand your question, do you mean whether this could have worked: `anything = self.m1 + self.m2` etc ? – Tacratis Jan 16 '19 at 05:39
  • Yeah!!. Why is it that only m1 = self.m1 + self.m2 works but a1 = self.m1 + self.m2 gives me an error? – Bhargav Mg Jan 16 '19 at 05:41
  • 1
    @BhargavMg Are you sure? Please send the issue code, and the error you get. – atline Jan 16 '19 at 05:44
  • Please find the below code. – Bhargav Mg Jan 16 '19 at 05:46
  • 1
    "`m1 = self.m1 + self.m2` works but `a1 = self.m1 + self.m2` gives me an error" is only possible if you forgot to change all references of `m1` into `a1`, e.g. `Student(m1,m2)` to `Student(a1,m2)`. – TigerhawkT3 Jan 16 '19 at 05:46
  • class Student: def __init__(self,m1,m2): self.m1 = m1 self.m2 = m2 def __add__(self, other): a1 = self.m1 + self.m2 a2 = other.m2 + other.m2 a3 = Student(a1,a2) return a3 s1 = Student(50,60) s2 = Student(70,80) s3 = s1 + s2 print (s3.a1) print (s3.a1) AttributeError: 'Student' object has no attribute 'a1' – Bhargav Mg Jan 16 '19 at 05:46
  • 1
    1) Put that code into the question, not a comment. 2) Why do you think `s3` has an `a1` when you didn't save it anywhere? – TigerhawkT3 Jan 16 '19 at 05:47

1 Answers1

-1

the name doesn't matter, it is just convention to make it more readable, this should work just as well:

def __add__(self, other):
    a1 = self.m1 + self.m2 
    m2 = other.m2 + other.m2
    s3 = Student(a1,m2)
    return s3

do pay attention that you have to modify m1 twice (but leave them untouched in self and other)

edit: there appears to be some mistake in the code, as m2 appears 3 times and m1 appears only once

Tacratis
  • 1,035
  • 1
  • 6
  • 16
  • Hello please find the below code. class Student: def __init__(self,m1,m2): self.m1 = m1 self.m2 = m2 def __add__(self, other): a1 = self.m1 + self.m2 a2 = other.m2 + other.m2 a3 = Student(a1,a2) return a3 s1 = Student(50,60) s2 = Student(70,80) s3 = s1 + s2 print (s3.a1) print (s3.a1) AttributeError: 'Student' object has no attribute 'a1' – Bhargav Mg Jan 16 '19 at 05:49
  • Ok, if that is what you mean, then yes, the names of class members are defined in the `__init__`, and they cannot be changed. `a1` and `a2` were only temporary variables stored during `__add__` and then trashed when it is done running – Tacratis Jan 16 '19 at 05:54
  • Got it. Thank you:) – Bhargav Mg Jan 16 '19 at 05:58