我可以使用对象(类的实例)作为 Python 中的字典键吗?

发布于 2024-12-06 18:01:15 字数 235 浏览 2 评论 0原文

我想使用类实例作为字典键,例如:

classinstance = class()
dictionary[classinstance] = 'hello world'

Python似乎无法将类作为字典键处理,还是我错了? 另外,我可以使用像 [(classinstance, helloworld),...] 这样的元组列表来代替字典,但这看起来非常不专业。 您有解决该问题的任何线索吗?

I want to use a class instance as a dictionary key, like:

classinstance = class()
dictionary[classinstance] = 'hello world'

Python seems to be not able to handle classes as dictionary key, or am I wrong?
In addition, I could use a Tuple-list like [(classinstance, helloworld),...] instead of a dictionary, but that looks very unprofessional.
Do you have any clue for fixing that issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乱了心跳 2024-12-13 18:01:15

您的实例必须是可哈希的。 Python 术语表告诉我们:

如果一个对象具有在其生命周期内永不改变的哈希值(它需要一个 __hash__() 方法),并且可以与其他对象进行比较(它需要一个 __eq__()__cmp__() 方法)。比较相等的可哈希对象必须具有相同的哈希值。

哈希性使得对象可以用作字典键和集合成员,因为这些数据结构在内部使用哈希值。

Python 的所有不可变内置对象都是可哈希的,但可变容器(例如列表或字典)却不是。默认情况下,作为用户定义类实例的对象是可哈希的;它们比较不相等,它们的哈希值就是它们的id()。

Your instances need to be hashable. The python glossary tells us:

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() or __cmp__() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

人事已非 2024-12-13 18:01:15

以下代码效果很好,因为默认情况下,您的类对象是可散列的:

Class Foo(object):
    def __init__(self):
        pass

myinstance = Foo()
mydict = {myinstance : 'Hello world'}

print mydict[myinstance]

输出
你好世界

此外,为了更高级的用法,您应该阅读这篇文章:

自定义类型作为字典的对象键

The following code works well because by default, your class object are hashable :

Class Foo(object):
    def __init__(self):
        pass

myinstance = Foo()
mydict = {myinstance : 'Hello world'}

print mydict[myinstance]

Output :
Hello world

In addition and for more advanced usage, you should read this post :

Object of custom type as dictionary key

梦忆晨望 2024-12-13 18:01:15

尝试在您的类中实现 hasheq 方法。

例如,这是我制作的一个简单的可哈希字典类:

class hashable_dict:
    def __init__(self, d):
        self.my_dict = d
        self.my_frozenset = frozenset(d.items())
    def __getitem__(self, item):
        return self.my_dict[item]
    def __hash__(self):
        return hash(self.my_frozenset)
    def __eq__(self, rhs):
        return isinstance(rhs, hashable_dict) and self.my_frozenset == rhs.my_frozenset
    def __ne__(self, rhs):
       return not self == rhs
    def __str__(self):
        return 'hashable_dict(' + str(self.my_dict) + ')'
    def __repr__(self):
        return self.__str__()

Try implementing the hash and eq methods in your class.

For instance, here is a simple hashable dictionary class I made:

class hashable_dict:
    def __init__(self, d):
        self.my_dict = d
        self.my_frozenset = frozenset(d.items())
    def __getitem__(self, item):
        return self.my_dict[item]
    def __hash__(self):
        return hash(self.my_frozenset)
    def __eq__(self, rhs):
        return isinstance(rhs, hashable_dict) and self.my_frozenset == rhs.my_frozenset
    def __ne__(self, rhs):
       return not self == rhs
    def __str__(self):
        return 'hashable_dict(' + str(self.my_dict) + ')'
    def __repr__(self):
        return self.__str__()
那支青花 2024-12-13 18:01:15

使用实例作为字典键没有任何问题,只要它遵循规则:字典键必须是不可变的。

There is nothing wrong with using an instance as a dictionary key so long as it follows the rules: A dictionary key must be immutable.

岁月无声 2024-12-13 18:01:15

您可以创建一个像“Strategy”这样的文件夹,然后您可以使用 pickle 来保存和加载类的对象。

import pickle
import os

# Load object as dictionary ---------------------------------------------------
def load_object():
    file_path = 'Strategy\\All_Pickles.hd5'
    if not os.path.isfile(file_path):
        return {}
    with open(file_path, 'rb') as file:
        unpickler = pickle.Unpickler(file)
        return dict(unpickler.load())


# Save object as dictionary ---------------------------------------------------
def save_object(name, value):
    file_path = 'Strategy\\All_Pickles.hd5'
    object_dict = load_object()
    with open(file_path, 'wb') as file:
        object_dict[name] = value
        pickle.dump(object_dict, file)
        return True


class MyClass:
    def __init__(self, name):
        self.name = name

    def show(self):
        print(self.name)


save_object('1', MyClass('Test1'))
save_object('2', MyClass('Test2'))
objects = load_object()
obj1 = objects['1']
obj2 = objects['2']
obj1.show()
obj2.show()

我创建了一个类的两个对象并调用了该类的方法。
我希望,它可以帮助你。

You can create a folder like 'Strategy' then you can use pickle to save and load the objects of your class.

import pickle
import os

# Load object as dictionary ---------------------------------------------------
def load_object():
    file_path = 'Strategy\\All_Pickles.hd5'
    if not os.path.isfile(file_path):
        return {}
    with open(file_path, 'rb') as file:
        unpickler = pickle.Unpickler(file)
        return dict(unpickler.load())


# Save object as dictionary ---------------------------------------------------
def save_object(name, value):
    file_path = 'Strategy\\All_Pickles.hd5'
    object_dict = load_object()
    with open(file_path, 'wb') as file:
        object_dict[name] = value
        pickle.dump(object_dict, file)
        return True


class MyClass:
    def __init__(self, name):
        self.name = name

    def show(self):
        print(self.name)


save_object('1', MyClass('Test1'))
save_object('2', MyClass('Test2'))
objects = load_object()
obj1 = objects['1']
obj2 = objects['2']
obj1.show()
obj2.show()

I created two objects of one class and called a method of the class.
I hope, it can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文