What is the expected behavior of the following code?
What is the expected behavior of the following code?
A
Answer is A. The instruction object_1.Variable += 1 creates a new attribute to object_1 and it does not modify class variable 'Variable': object_1.__dict__ ==> {'value':0, 'variable':1} Since class variable 'Variable' is still 0, object2.variable will be 0, then: object_2.Variable (0) + object_1.value (1) = 0 + 1 = 1
i don't understand , variable is a class variable...how is object2.variable = 0?
'Variable' is a class variable to begin with. But then, obejct_1.Variable is created and assigned a value of 1. Obejct_2.Variable, which is also an instance variable, has had no value assigned to it.
print(o_2.Variable, o_1.value, o_1.Variable, o_2.value) #0 0 1 1
Isn't it 0 (object_2.Variable) + 1 (oject_1.value) = 1?
can anyone please explain how the result = 0 please
run this code class Class: Variable = 0 def __init__(self): self.value = 0 object_1 = Class() object_1.Variable += 1 object_2 = Class() object_2.value += 1 print(object_2.Variable + object_1.value)
AttributeError Traceback (most recent call last) Cell In[15], line 10 8 object_1.Variable += 1 9 object_2 = Class() ---> 10 object_2.value += 1 12 print(object_2.Variable + object_1.value) AttributeError: 'Class' object has no attribute 'value' This is what i am getting
A. it outputs 0