Answer: To set an attribute
Answer: To set an attribute
Answer: Special methods
def Demo:
def __init__(self):
__a = 1
self.__b = 1
self.__c__ = 1
__d__= 1
Answer: __b
Answer: when no exception occurs
def f(x):
yield x+1
print("test")
yield x+2
g=f(9)
Answer: No output
int('65.43')
Answer: ValueError
class A():
def disp(self):
print("A disp()")
class B(A):
pass
obj = B()
obj.disp()
Answer: A disp()
Answer: The __eq(other) method is defined in the object class
Answer: __eq__()
class Demo:
def __init__(self):
self.x = 1
def change(self):
self.x = 10
class Demo_derived(Demo):
def change(self):
self.x=self.x+1
return self.x
def main():
obj = Demo_derived()
print(obj.change())
main()
Answer: An exception is thrown
Answer: An object
class test:
def __init__(self,a="Hello World"):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
Answer: “Hello World” is displayed
Answer: To access the attribute of the object
class change:
def __init__(self, x, y, z):
self.a = x + y + z
x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)
Answer: 7
class test:
def __init__(self,a):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
Answer: Error as one argument is required while creating the object
advertisement
class test:
def __init__(self):
self.variable = 'Old'
self.Change(self.variable)
def Change(self, var):
var = 'New'
obj=test()
print(obj.variable)
Answer: ‘Old’ is printed
Answer: Creating an instance of class
class fruits:
def __init__(self, price):
self.price = price
obj=fruits(50)
obj.quantity=10
obj.bags=2
print(obj.quantity+len(obj.__dict__))
Answer: 13