What is the output of the following code?
class class1:
a = 1
def f1(self):
a = 2
class1.a += 1
print(class1.a, end=' ')
print(a, end=' ')
class1().f1()
class1().f1()
2 3 3 2
2 2 2 2
2 2 3 2
2 2 3 3
Answers
Answered by
0
Python Program
Answer for the code is:
2 2 3 2
To learn more...
brainly.in/question/7867534
Answered by
0
The output of the code
class class1:
a = 1
def f1(self):
a = 2
class1.a += 1
print(class1.a, end=' ')
print(a, end=' ')
class1().f1()
class1().f1()
is :
2 2 3 2
- When the first method class1().f1() was called then initially 2 was printed because "class1.a =1" and according to the code
class1.a += 1 ,
which gives class1.a = 2
- After that again 2 is printed as "a = 2" in the f1 block.
- When the second method class1().f1() was called then class1.a =3 because class1.a becomes 2 after the first method was called.
- After that 2 is printed because still a is equal 2 in the f1 block.
Similar questions