+ 1

How to add to lists in parent class attributes?

class MyClass: A_list = [] #Intialized with some attributes def __init__(self): pass #making the list def Add_to_list(self, *names): for i in names: MyClass.A_list.append(I) #now I want to clear the list def Clear_list(self): for i in MyClass.A_list: del I Doesn't work. class Second_class(MyClass): B_list = MyClass.A_list.extend([1, 2 ,3] #i'm trying to add the list in the parent class to this one but it doesn't work

20th Jul 2021, 2:28 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
3 odpowiedzi
+ 1
https://stackoverflow.com/questions/3475488/static-variable-inheritance-in-JUMP_LINK__&&__python__&&__JUMP_LINK Should be close to what you're looking for. Class attributes can be tricky to deal with when inheritance is involved, mostly because they can't be treated like inherited members.
20th Jul 2021, 2:56 PM
BootInk
BootInk - avatar
+ 1
NotAPythonNinja Thanks. I forgot about the clear method. As for the second one, I just noticed the extend method returns None. But what I actually want is to add the A_list to Another list([1,2,3]) and form a new list B_list
20th Jul 2021, 3:12 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
+ 1
BootInk . Thanks man. This helped. I'll try using type(self) in conjunction with the extend method. Hope it works
20th Jul 2021, 3:17 PM
Nwalozie Elijah
Nwalozie Elijah - avatar