What is setattr() used for?
What is getattr() used for?
What is the output of the following code ?
class AttrTest:
def __init__(self, a, b, c):
self.X = a + b + c
p = AttrTest(7, 8, 9)
b = getattr(p, 'X')
setattr(p, 'X', b + 1)
print(p.X)
What is hasattr() used for?
What is the output of the following code ?
class AttrTest:
def __init__(self, a, b):
self.a = a
self.b = b
obj = AttrTest(34, 'S')
obj.X = 7
print(hasattr(obj, 'X'))
What is the output of the following code ?
class A:
def __init__(self):
self.X = 10
a = A()
a.X = 20
delattr(a, 'X')
print(a.X)
What is the output of the following code ?
class A:
x = 10
def __getattr__(self, item):
print("__getattr__ is called")
a = A()
a.Y
What is the output of the following code ?
class A:
X = 10
def __getattribute__(self, item):
print("__getattribute__ is called")
a = A()
print(a.X, a.Y)
What is the output of the following code ?
class A:
X = 10
def __setattr__(self, key, value):
print("__setattr__ is called")
a = A()
a.Y = 10
A.k = 90
print(a.X, a.k)
What is the output of the following code ?
class A:
X = 10
def __getattribute__(self, item):
print("__getattribute__ is called")
def __getattr__(self, item):
print("__getattr__ is called")
a = A()
a.Y