-4
class Test:
    def __init__(self):
        self.i = "Test"
        self.o = f"{self.i} Test"
    

test = Test()
print(test.o)
test.i = "Hello"

If I change the value of the attribute test.i, wouldn't the class look like that (just for the object test)?

class Test:
    def __init__(self):
        self.i = "Test"
        self.o = f"{self.i} Test"
        self.i = "Hello" 
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
TheEnd1278
  • 47
  • 4

2 Answers2

1

By changing the value of test.i to Hello will not change the value of i of the class .

test is an object of class Test and you are only changing the i value of test object only.

You need to know the difference between a Class and an Object.

Please go through this

Ram
  • 4,724
  • 2
  • 14
  • 22
0

If I understand your question well:

by doing test.i = "Hello", your are modifying its attribute i but your are not "modifying the class" nor its code. Your are simply changing the value of a variable that happens to be an object attribute.

So, the class "does not look like that" because it does not mean anything.

Moreover, when you change test.i after instantiating the class like you did, the value of test.o will not be changed.

See What's the pythonic way to use getters and setters? : you can use @property to change test.o when test.i is modified.

class Test:
    def __init__(self):
        self.i = "Test"

    @property
    def o(self):
        return f"{self.i} Test"
    

test = Test()
print(test.o)  # 'Test Test'
test.i = "Hello"
print(test.o) # 'Hello Test'

Keep in mind that Python only does what YOU tell it to do. If you tell it to change the value of test.i, test.i will have the new value, but python will not modify test.o unless you tell it to do so.

With the way you defined your class, when you create a new object by doing test = Test(), Python initializes it with the value you put in __init__().

Then, when you change test.i by doing test.i = "Hello", you only change this value and not the other properties of the class.

Think about it this way : if I have an object and I modify one of its attributes, i do not want the other to be modified unless I tell Python to do so... This would be an unwanted side effect.

The @property decorator allow you to define a function and then use it as a normal attribute. In this example, test.o is re-computed each time you ask for it with the value of test.i.

Side note : those are terrible variable names...

  • Yeah, because the value of test.o wont change, I wanted to ask, if i overwrite the variable test.i or just change the value. If i overwrite the value, test.o wouldnt change. – TheEnd1278 Jul 08 '21 at 09:01
  • you can change your class so that it changes when you change `test.i`. see my updated answer – Louis-Justin Tallot Jul 08 '21 at 09:04
  • Yeah but I still dont understand, why If i change the value of test.i to "hello" test.o doesnt change. Idk if im dumb but thats why I ask, that If i change the value of test.i, does it overwrite the test.i or just change the value. For Example: test.i = "Test" will change to test.i = "Hello" or test.i = "Test" test.i = "Hello" In the second example im overwriting test.i – TheEnd1278 Jul 08 '21 at 09:15
  • please edit your question and try to be a bit clearer :) (punctuation could be useful, especially in your last comment) – Louis-Justin Tallot Jul 08 '21 at 09:17
  • Ok, im tryng to be a bit clearer. My question is, if i change the value of the variable test.i, like in the example above, why does the value of test.o not change? And I think its since im overwriting the variable test.i after setting the value of test.o, so test.i changes, but test.o dont. – TheEnd1278 Jul 08 '21 at 09:23
  • And if i use a second function, where I return that f string, then Im going to use the overwritten value of test.i – TheEnd1278 Jul 08 '21 at 09:24
  • See my updated answer. Please also update your question with the content of your comments (questions should not be in comments but rather in the question body) – Louis-Justin Tallot Jul 08 '21 at 09:28
  • Yeah Ive seen it, thats why I asked that question. Im a bit confused rn :( – TheEnd1278 Jul 08 '21 at 09:31