2017-06-19 67 views
-1

是否有可能創建一個類的新對象本身(在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) + "]" 
+1

你呈現不創建test'的'一個完全獨立的實例代碼。雖然這個例子有點沒有意義,因爲'test'沒有任何屬性。你爲什麼認爲它不起作用?你打算用'new'做什麼? (順便說一下,你的'__init__'有語法錯誤。) – 5gon12eder

+0

你的代碼工作正常。你試過了嗎? –

+0

@ 5gon12eder,因爲我試着在一個真實的代碼,並試圖改變屬性值,並沒有工作 – james

回答

4

是否有可能在自己創建一個類的新對象(蟒蛇)?

是的。這完全有可能。

下面是一個例子:

>>> 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 
>>> 
+0

這是一個普遍的答案。謝謝 ! – james

+0

很高興幫助@詹姆斯。 –

1

這看起來像一個deque(雙端隊列)和切片操作不正確的退出顯然無關。你需要在l1之前和l2之後修正該項目上的prev/next鏈接。

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 
+0

這確實清理了他的代碼。榮譽。但是這似乎並沒有真正回答他的_main_問題:_「是否有可能創建一個類的新對象(在python中)?」_。 –

+0

但我認爲這將改變名單本身。切片函數應該返回想要的部分,實際列表將被更新。例如:list = [1,2,3,4] list.slice(2,3)返回[2,3],然後列表將是[1,4] – james

+0

代碼的概念很好,我發現了一些問題,例如切片(整個列表)或僅僅是第一個元素列表(a,a)。我想更多的是,如果(S)會完成這項工作 – james

相關問題