您可以检查两个对象实例是否相同

时间:2019-07-05 23:46:53

标签: python python-3.x

在这里定义的LinkedList类中,我想检查是否可以检查self.head ==节点,或者需要将节点与所有属性进行比较并显式定义等于方法?我看到有人在没有等于方法

的情况下使用此代码
class Node(object):
    def __init__(self,key=None,value=None):
        self.key = key
        self.value = value
        self.previous = None
        self.next = None

class LinkedList(object):
    def __init__(self):
        self.head = None
        self.tail = None
        self.count = 0

    def prepend(self,value):
        node = Node(value)
        if self.head is None:
            self.head = node
            self.tail = self.head
            self.count = 1
            return

        self.head.previous = node
        node.next = self.head
        self.head = node
        self.count += 1

1 个答案:

答案 0 :(得分:1)

要查看from bs4 import BeautifulSoup import urllib3 http = urllib3.PoolManager() url = 'https://pub.fsa.gov.ru/ral/view/8/applicant' response = http.request('GET', url) soup = BeautifulSoup(response.data) print(soup) 的地址是否与obj1的地址匹配,请使用obj2运算符。

您已经在此测试中这样做了:

is

if self.head is None: 类中只有一个对象(单例), 并且您实际上是在问NoneType是否与id(self.head)的{​​{1}}或地址匹配。

随意与其他链接列表节点对象进行此操作。

如果是OTOH,您是要问id(), 这很可能会问节点属性None是否在两者中都匹配, 和属性self.head == some_node都匹配, 根据您的课堂方法, 例如使用a。 由浅表副本创建的节点可能b等于原始节点, 但是def __eq__会显示已为其分配了单独的存储空间。

相关问题