collection of instance | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

collection of instance

I have just learned about classes. if i want to have a collection of intances, what data type should i use best?

1st Nov 2018, 3:45 AM
yuri
yuri - avatar
1 Answer
+ 2
Just like any collection, it depends on what type of objects you have and what you want to do with them. List is usually the default option. But if they are all distinct, and the order doesn't matter to you, a set could be a good choice. Sets have faster membership testing. Tuple is also an option if you know you won't need to modify the collection. For sets, the objects of the class you define need to be hashable, and the docs seem to say that they are by default hashable: "Objects which are instances of user-defined classes are hashable by default. They all compare unequal (except with themselves), and their hash value is derived from their id()." https://docs.python.org/3/glossary.html
1st Nov 2018, 11:43 AM
Kishalaya Saha
Kishalaya Saha - avatar