+ 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 = []
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.
+ 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..)
+ 1
Jayakrishnađźđł Whats the difference?
+ 1
Why not have it as a function
+ 1
Put 3 parameter in it or 4
Return list
+ 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
0
Ion Kare
class List:
def listing():
âŠ.
Like this?
0
Hi