निम्नलिखित पायथन प्रोग्राम का आउटपुट खोजें।
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
111
B
555
C
666
D
error in code
Explanation
- The
__init__method takes a parameterid, and assigns that value to the instance attributeself.id. - The line
id = 555modifies the localidvariable 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
idinside the__init__method is a local variable. Changing it doesn't affect the instance variableself.id. - So, the value of
self.idremains as111even thoughid = 555is set within the constructor.self.id = idइंस्टेंस वेरिएबल को असाइन करता है। अगली लाइनid = 555केवल एक लोकल वेरिएबल को बदलती है, क्लास के self.idको नहीं। इसलिएacc.idका मान 111 ही रहेगा।
Correct Answer: A) 111