Home > Blockchain >  Python: how to utilize instances of a class
Python: how to utilize instances of a class

Time:11-15

New to OOP and python, I am struggling enormously to grasp what good classes actually are for. I tried to ask help from a lecturer who said "oh, then you should read about general methods to classes". Been putting in a days work but get no where.

I get it that a class allow you to collect an instance structure and methods to it, like this:

class Items:
    def __init__(self, item_id, item_name):
        self.item_id = item_id
        self.item_name = item_name
    def show_list(self):
        print(self.item_id, self.item_name)
idA = Items("idA", "A")
idA.show_list()

But what is even the point of a class if there were not MANY instances you would classify? If I have a method within the class, I must hard code the actual instance to call the class for. What if you want a user to search and select an instance, to then do operations to (e.g. print, compute or whatever)??

I thought of doing it like this:

class Items:
    def __init__(self, item_id, item_name):
        self.item_id = item_id
        self.item_name = item_name
    def show_list(self):
        print(self.item_id, self.item_name)
idA = Items("idA", "A")
idB = Items("idB", "B")
select_item = input("enter item id")
select_item.show_list()

Replacing hard coded variable with input variable doesn't work, probably logically. I then played with the idea of doing it like this:

class Items:
    def __init__(self, item_id, item_name):
        self.item_id = item_id
        self.item_name = item_name
iL = [Items('idA', 'A'), Items('idB', 'B')]
selected_item = input("enter item id")
for selected_item in iL:
    print(f'{selected_item.item_id} {selected_item.item_name}')

Now all are called thanks to making it a list instead of separate instances, but how do I actually apply code to filter and only use one instance in the list (dynamically, based on input)?

I would love the one who brought me sense to classes. You guys who work interactively with large data sets must do something what I today believe exist in another dimension.

See examples above^^

CodePudding user response:

It seems you want to find all the instances of a certain element within a class.

This is as simple as:

print([x for x in iL if x.item_id == selected_item])

Now, you may ask why you can't just store the elements of iL as tuples instead of classes. The answer is, you can, but

("idA", "A")

is much less descriptive than:

item_id = "idA"
item_name = "A"

Any code you write with classes, you should in theory be able to write without classes. Classes are for the benefit of the coder, not the end-user of the program. They serve to make the program more readable, which I'm sure you'll find is a desirable property.

CodePudding user response:

Your point here is to lookup for Items instances based on their item_id attribute.

That's a thing to create instances of a class.

It's a completely different thing to search for items objects stored in memory - that is not directly linked to the concept of OOP, classes and instances.

You could use dictionary to store references of your objects and then lookup in your dictionary.

class Items:
    def __init__(self, item_id, item_name):
        self.item_id = item_id
        self.item_name = item_name
    def show_list(self):
        print(self.item_id, self.item_name)

idA = Items("idA", "A")
idB = Items("idB", "B")
lookup_dict = {"idA": idA, "idB": idB}
select_item = input("enter item id")
found_item = lookup_dict.get(select_item)
if found_item:
    found_item.show_list()
else:
    print(f"item {select_item} not found")
  • Related