निम्नलिखित पायथन प्रोग्राम का आउटपुट खोजें।
Find the output of the following Python programs.
class Acc:
def __init__(self, id):
self.id = id
id = 555
acc = Acc(111)
print (acc.id)
A)
B)
C)
D)
Explanation:
- The
__init__
method takes a parameterid
, and assigns that value to the instance attributeself.id
. - The line
id = 555
modifies the localid
variable in the constructor, but does not affect the instance variableself.id
. - When you print
acc.id
, it will print the value that was passed into the constructor, which is111
.
Output:
Explanation:
- The
id
inside the__init__
method is a local variable. Changing it doesn't affect the instance variableself.id
. - So, the value of
self.id
remains as111
even thoughid = 555
is set within the constructor.