How do i put lists inside a class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do i put lists inside a class?

Like this? class Lists : Ownerlist = [] Petlist = [] Temp = [] Op = [] #is this also correct: class Pet : Ownerlist = [] Petlist = [] Temp = [] Op = [] def __init__(self, name, age, gender) self.name = name self.age = age self.gender = gender #Or class Pet: def __init__(self, name, age, gender) self.name = name self.age = age self.gender = gender self.Ownerlist = [] self.Petlist = [] self.Temp = [] self.Op = []

11th Apr 2022, 12:22 PM
Lenoname
8 Answers
+ 4
Both ways are valid. If you want it as class variable use Lists class data way, If want those as instance data, then use 3rd way.
11th Apr 2022, 12:57 PM
Jayakrishna 🇮🇳
+ 2
print(Lists.Ownerlist) # class variable has same values for every instance, while instance variable has its own copies of values separatly, for each object. print(Pet.Ownerlist) #class variable acces through class name print(Pet1("a",5,"m").Ownerlist) #instance way access.. , through object .. #if you know static data in other languages, then its almost same. (but not completely..)
11th Apr 2022, 1:32 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Whats the difference?
11th Apr 2022, 1:23 PM
Lenoname
+ 1
Why not have it as a function
11th Apr 2022, 4:04 PM
Ion Kare
Ion Kare - avatar
+ 1
Put 3 parameter in it or 4 Return list
11th Apr 2022, 4:35 PM
Ion Kare
Ion Kare - avatar
+ 1
Class List(Pet): def __init__(self, list_owner, list_pet): super()__init__() self.list_owner = list_owner self.list_pet = list_pet def list_object(self): self.list_owner = [self.Ownerlist , ] self.list_pet = [self.Petlist , ] return self.list_owner, self.list_pet Something like this
11th Apr 2022, 4:45 PM
Ion Kare
Ion Kare - avatar
0
Ion Kare class List: def listing(): …. Like this?
11th Apr 2022, 4:30 PM
Lenoname
0
Hi
12th Apr 2022, 4:55 AM
Mory Dembele
Mory Dembele - avatar