+ 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)

24th Oct 2018, 6:37 AM
AMAN TOMAR
AMAN TOMAR - avatar
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
24th Oct 2018, 8:06 AM
jtrh
jtrh - avatar
+ 3
Kuba Siekierzyński thank u .done ..
24th Oct 2018, 9:20 AM
AMAN TOMAR
AMAN TOMAR - avatar
+ 2
jtrh yeah .made a mistake there.
24th Oct 2018, 8:58 AM
AMAN TOMAR
AMAN TOMAR - avatar
+ 2
jtrh thanks for helping
24th Oct 2018, 8:58 AM
AMAN TOMAR
AMAN TOMAR - avatar
+ 2
AMAN TOMAR You might mark jtrh's answer as best, as it covers your question fully :)
24th Oct 2018, 9:17 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
24th Oct 2018, 6:38 AM
AMAN TOMAR
AMAN TOMAR - avatar