+ 1
Help me with the logic
class Parent(object1): x = 1 class Child1(Parent): pass class Child2(Parent): pass Child1.x = 2 Parent.x = 3 print(Parent.x) print(Child1.x) print(Child2.x)
6 Antworten
+ 2
I think you meant 'class Parent(object)' which is the default base for all classes instead of object1 which is not defined.
Child1 and Child2 classes share a base class of Parent, meaning they can inherit their parent's members and methods.
Parent has a static member x which was assigned to 1. When Child1.x was assigned to 2, it no longer follows the parent's x. On the other hand Child2.x has not been reassigned so it still keeps its parent's x.
After Parent.x was reassigned to 3, the results should be:
3
2
3
+ 3
Kuba Siekierzyński thank u .done ..
+ 2
jtrh yeah .made a mistake there.
+ 2
jtrh thanks for helping
+ 2
AMAN TOMAR You might mark jtrh's answer as best, as it covers your question fully :)