-1

This is the code where I make two classes. In first I create email by arguments, in Second I create email by attributes. when I change the attributes manually they both show the same result of not updating the email? What am I missing here?

class Nam:
  def __init__(self, first, last):
    self.first = first
    self.last = last
    self.email = first + "." + last + "@gmail.com"

e = Nam("Ridhi","Shah")
print(e.first)
e.email

Ridhi

Ridhi.Shah@gmail.com

e.first = "Neha"
print(e.first)
e.email

Neha

Ridhi.Shah@gmail.com

Second Class
class Nam2:
  def __init__(self, first, last):
    self.first = first
    self.last = last
    # This is the line different from first class
    self.email = self.first + "." + self.last + "@gmail.com"

e2 = Nam2("Nidhi","Shah")
print(e2.first)
e2.email

Nidhi

Nidhi.Shah@gmail.com

e2.first = "Sneha"
print(e2.first)
e2.email

Sneha

Nidhi.Shah@gmail.com

This class is still giving Nidhi. even after I used self.

This is the code. Please Check!

  • Posting your code as an image makes it harder for us to help. Please update the question to add the code as plain text, and remove the images. – John Gordon Oct 11 '22 at 23:56
  • 1
    Setting `self.email` does not define an ongoing relationship. It merely captures the current value of the right hand side, and sets the left hand side to that value. Just like any other assignment. It's not some kind of ongoing differential equation. Once executed, that statement is finished, unless you call `__init__` again (which would be very abnormal in an existing class instance). – Tom Karzes Oct 12 '22 at 00:03
  • The email address is only set once, when the Nam object is created. It is never updated after that. So the email address will remain its original value, no matter how you change the first/last names. – John Gordon Oct 12 '22 at 00:13
  • Thanks @TomKarzes. But I changed self.first in the second class which is on the right hand side. What can I do if I don't want to create a new instance of this class, and change the email just by changing the first attribute? – Anirudh Upadhyay Oct 12 '22 at 00:14
  • Thanks @JohnGordon. Makes sense now. Goes same for Nam2? – Anirudh Upadhyay Oct 12 '22 at 00:17
  • Welcome to Stack Overflow. It is the same as if there were no classes involved. Notice how if you write `a = 1` and then `b = 2` and then `c = a + b`, now `c` is equal to `3`; now if you change `a`, `c` does not change. It has nothing to do with classes or attributes. – Karl Knechtel Oct 12 '22 at 00:33
  • Thanks @KarlKnechtel for the apt answer. Crystal clear now. – Anirudh Upadhyay Oct 12 '22 at 00:42
  • Please also make sure to check the linked duplicate. – Karl Knechtel Oct 12 '22 at 00:45

1 Answers1

0

I think you want email to be a property that is re-calculated each time it is used, instead of a single unchanging value.

class Nam:
    def __init__(self, first, last):
        self.first = first
        self.last = last

    @property
    def email(self):
        return self.first + "." + self.last + "@gmail.com"
John Gordon
  • 29,573
  • 7
  • 33
  • 58