I started to learn Python Class (OOP).
I have example code with commented question...
Can Anyone explained to me my questions in comment?
I do not understaind some pieces of class variables...
This is the code:
Code: Select all
#!/usr/bin/env python3 class A: def __init__(self): self.__num = 1 print("1. Init num: ", self.__num) def setNum(self,value): self.__num = value class B: def __init__(self, class_a): BA = class_a #print("class B is getting __num from class A: ",BA.__num) # This is not working, why??? print("3. class B is getting __num from class A: ",BA._A__num) # Why this is 1? Why not 5? Is true that class B is inputing parameter instance of class A or not? # If BA._A__num is from A self.__num, what is BA.__num? How this is not same? BA.__num = 88 print("4. New __num from class A: ",BA.__num) BA._A__num = 33 print("5. New _A__num from class A: ",BA._A__num) def main(): a = A() # init num is printed a.__num = 5 print("2. Print num: ",a.__num) b = B(a) # Init num from class B is called, it prints __num from class A print("6. Final __num form class A:",a.__num) # This I don't understaind!!! Why __num is not 88? print("7. Final __num as _A__num:",a._A__num) # If __num is not __num 88 from class B why this _A__num is 33 like in class B? a.setNum(45) print("8. Final __num after A.setNum procedure:",a.__num) # OMG, why __num is not initiated with 45? print("9. Final _A__num:",a._A__num) # instead of this one line up, this is initiated with 45, why this? if __name__ == '__main__': main()