简体   繁体   English

Python:如何使用 class 的实例

[英]Python: how to utilize instances of a class

New to OOP and python , I am struggling enormously to grasp what good classes actually are for . OOP 和 python 的新手,我非常努力地想掌握好课程的实际用途 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 允许您收集实例结构和方法,如下所示:

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?但是,如果您要分类的实例不多,那么 class 的意义何在? If I have a method within the class, I must hard code the actual instance to call the class for.如果我在 class 中有一个方法,我必须硬编码实际实例来调用 class。 What if you want a user to search and select an instance, to then do operations to (eg print, compute or whatever)??如果您希望用户搜索和 select 一个实例,然后执行操作(例如打印、计算或其他)怎么办?

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^^见上面的例子^^

It seems you want to find all the instances of a certain element within a class.似乎您想在 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.现在,您可能会问为什么不能将iL的元素存储为元组而不是类。 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.它们有助于使程序更具可读性,我相信您会发现这是一个理想的特性。

Your point here is to lookup for Items instances based on their item_id attribute.您的意思是根据 Items 的item_id属性查找Items实例。

That's a thing to create instances of a class.这就是创建 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.搜索存储在 memory 中的项目对象是完全不同的事情——这与 OOP、类和实例的概念没有直接联系。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM