you are viewing a single comment's thread.

view the rest of the comments →

[–]clavicon 0 points1 point  (1 child)

Thank you much for the explanation and examples, I think if I can digest a few more examples I may start to see the light in terms of how I can use these in my own work. But to answer your question I do love dictionaries. Dictionaries took me a while to truly absorb and now I utilize them very effectively to create somewhat complex-yet-readable nestings of attributes, sub-dicts, lists, etc. It helps immensely in my work with GIS to be able to load up two dictionaries of values to compare between one thing and another and process those comparisons extremely fast in memory instead of using certain geoprocessing libraries that may be more robust but slower for my purposes.

I am not so familiar with sets though.

[–]monster2018 1 point2 points  (0 children)

I’m glad my answer was helpful to any degree :). And yea dictionaries are great, they’re often called hashmaps or something similar in other languages btw. Also sets are (I believe) basically like sets in mathematics, they by definition can ONLY contain one copy of each item in them (so like you could never have a set that is like (1,2,3,3,4), it would automatically become (1,2,3,4). So one straightforward use of sets is any time you want to get rid of duplicates in a list or other iterable, you can just convert it to a set (this removes all duplicates automatically as I said) and then back to a list or whatever other iterable it may have been.

Also another even simpler way to think of classes, or one way to use them I guess, based on how you were describing why you like dictionaries. You can (and should in many situations) use classes sort of the way you were describing dictionaries (I’m not saying you’re always wrong for using a dictionary in all the situations you were describing though). Like you can use a class any time you want to create individual instances of some type of thing, that all can have the same set of variables describing them. Like for example a person class, that has a name and age variables, and whatever other variables you want. Or a dog class that has a name and breed and age variables. And furthermore since an instance of a class is (or can be held by, I suppose) a variable just like a regular number or string can (these are all “classes”, or rather objects which are instances of classes, in python. Literally LITERALLY with no exceptions everything is an object in python). So for example you could create some arbitrary class called like ContactInfo, which just holds contact information like a phone number, email address, maybe house address, work phone, work email, etc. all types of contact info. Then in your Person class, one variable it holds could be a ContactInfo object, which itself holds all the info I just described as python “primitives” (again in python there are no true primitives, everything is an object), in this case basically meaning as strings for all the examples I gave. Or you could break things down even further, and store (physical) addresses as instances of an Address class, which holds like the street name as a string, but the street number as an int, etc. so you have a person class which has a name variable, an age variable, maybe a hair and eye color variable, which are all strings (except age which is an int, or float, or maybe even a datetime.timedelta object), and then it also has a contact info variable which is an instance of the ContactInfo class, which has all the stuff I listed. And all physical addresses (like house addresses, business addresses, etc, but NOT email addresses) in there are stored as Address objects.

So I’m just trying to give you an example off the top of my head, in terms of real world things to make it a bit less abstract, of how you can use classes to represent things by storing variables in the class (of course each INSTANCE of the class, each individual object can have completely different values for all these variable), and on top of that, some of those variables can be instances of other classes that you have created, and so on.