In: Computer Science
Given the python code below, show output:
class MyClass:
i = ["1010"]
def f(self, name):
self.i.append(str("470570"))
self.name = name
return 'CPS'
if __name__ == "__main__":
x = MyClass()
print(x.f("U"))
print(x.name)
print(x.i)
y = MyClass()
print(y.f("D"))
print(y.name)
print(y.i)
Following code had some indentation issues, fixed those.
Following code does :
creates class name MyClass having instance variable i and
name
Have method called f : which takes name, and appends "470570" to i
and set self.name to name and returns 'CPS'
We have main method which does :
creates x (MyClass) object and y (MyClass) object.
x object calls f method with value "U" and prints as per
statements.
similar for object b.
PFB Source code :
----------------------
solution.py
----------------------
class MyClass:
i = ["1010"]
def f(self, name):
self.i.append(str("470570"))
self.name = name
return 'CPS'
if __name__ == "__main__":
x = MyClass()
print(x.f("U"))
print(x.name)
print(x.i)
y = MyClass()
print(y.f("D"))
print(y.name)
print(y.i)
----------------------
output
---------------------
---------------------
Let me know, if you face any issue.