[英]Creating new object of a class in the same class in python?
Is it possible to create an new object of a class in itself (in python) ? 是否可以自己创建一个类的新对象(在python中)?
In Order to explain the idea more i wrote this code which i don't think that it works. 为了进一步解释这个想法,我编写了这段代码,但我认为它不起作用。
The new object should be clearly independent from the current object(new attributes etc..) 新对象应明显独立于当前对象(新属性等)。
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) + "]"
Is it possible to create an new object of a class in itself (in python) ? 是否可以自己创建一个类的新对象(在python中)?
Yes. 是。 It is completely possible. 这是完全可能的。
Here is an example: 这是一个例子:
>>> 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)
>>>
Now you're probably thinking, "But the A
class hasn't been defined yet. Why isn't Python raising a NameError
?" 现在您可能会想, “但是尚未定义A
类。为什么Python不引发NameError
?” The reason this works it because of the way Python executes your code. 之所以起作用,是因为Python执行代码的方式。
When Python is creating the A
class, specifically the make_a
method, it sees that the identifier A
is being called. 当Python创建A
类(特别是make_a
方法)时,它会看到正在调用标识符A
It doesn't know whether A
is a function or class, or even if A
is defined . 它不知道A
是函数还是类,甚至A
都已定义 。 But it doesn't need to know. 但这不需要知道。
What A
is exactly is determined at run-time. A
的确切确定是在运行时确定的。 That is the only time Python checks whether A
is really defined or not. 这是Python唯一检查A
是否真正定义的时间。
This is also the reason why you are able to compile a function that references undefined variables: 这也是为什么您可以编译引用未定义变量的函数的原因:
>>> 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
>>>
That looks like a deque (double-ended queue) and the slice operation isn't quit right. 看起来像双端队列(双端队列),并且切片操作没有正确退出。 You need to fixup the prev/next links on the item before l1 and the one after l2. 您需要修复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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.