在 python 中的同一类中创建类的新对象



是否可以在自身(在python中(创建一个类的新对象?

为了更多地解释这个想法,我写了这段代码,我认为它不起作用。

新对象

应该明显独立于当前对象(新属性等(。

class LinkedList:
    def __init__(self):
        """ Construct an empty linked list. """
        self.first = None
        self.last = None
   def insert_before(self, new_item, next_item):
        """ Insert new_item before next_item. If next_item is None, insert
        new_item at the end of the list. """
        # First set the (two) new prev pointers (including possibly last).
        if next_item is not None:
            new_item.prev = next_item.prev
            next_item.prev = new_item
        else:
            new_item.prev = self.last
            self.last = new_item
        # Then set the (two) new next pointers (including possibly first).
        if new_item.prev is not None:
            new_item.next = next_item
            new_item.prev.next = new_item
        else:
            new_item.next = self.first
            self.first = new_item
    # assuming l1, l2 obj of class node with prev and next (attributes)
    def slice(self, l1, l2):
        curr = l1
        new = LinkedList()
        new.first = l1
        new.last = l2
        if l1.prev is not None:
           l2.prev = l1.prev
        else:
           self.first = l2.next
           self.first.prev = None
        if l2.next is not None:
           l1.next = l2.next
        else:
            self.last = l2.next
        Return new
class Node:
    def __init__(self, value):
    """ Construct an item with given value. Also have an id for each item,
    so that we can simply show pointers as ids. """
        Node.num_items += 1
        self.id = Node.num_items
        self.prev = None
        self.next = None
        self.value = value
    def __repr__(self):
    """ Item as human-readable string. In Java or C++, use a function like
    toString(). """
        return "[id = #" + str(self.id) 
           + ", prev = #" + str(0 if self.prev is None else self.prev.id) 
           + ", next = #" + str(0 if self.next is None else self.next.id) 
           + ", val = " + str(self.value) + "]"

是否可以在自身(在 python 中(创建一个类的新对象?

是的。这是完全可能的。

下面是一个示例:

>>> class A:
    def __init__(self, value):
        self.value = value
    def make_a(self, value):
        return A(value)
    def __repr__(self):
        return 'A({})'.format(self.value)

>>> a = A(10)
>>> a.make_a(15)
A(15)
>>> 

现在你可能会想,"但是A类还没有定义。为什么Python不提高NameError 之所以这样做,是因为Python执行代码的方式。

当 Python 创建 A 类(特别是 make_a 方法(时,它会看到标识符A正在被调用。它不知道A是函数还是类,甚至不知道是否定义了A。但它不需要知道。

A究竟是什么在运行时确定。这是Python唯一一次检查A是否真正定义。

这也是您能够编译引用未定义变量的函数的原因:

>>> def foo():
    a + b

>>> foo # Python compiled foo...
<function foo at 0x7fb5d0500f28>
>>> foo() # but we can't call it.
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    foo() # but we can't call it.
  File "<pyshell#7>", line 2, in foo
    a + b
NameError: name 'a' is not defined
>>> 

这看起来像一个双端队列,切片操作没有退出。您需要修复 l1 之前的项目和 l2 之后的项目上的上一个/下一个链接。

def slice(self, l1, l2):
    new = LinkedList()
    new.first = l1
    new.last = l2
    if l1.prev:
       l1.prev.next = l2.next
    if l2.next:
       l2.next.prev = l1.prev
    l1.prev = l2.next = None
    return new

相关内容

  • 没有找到相关文章

最新更新