簡體   English   中英

試圖返回一個字典,而不是得到一個

[英]trying to return a dict, get none instead

我正在嘗試創建一個 python 程序,該程序自動生成類文件,給定特定格式的輸入。 為了簡化數據的存儲和解析,我定義了一個名為 Class_Dict 的類,它包含單個類的規范。 輸入時,數據如下所示:

class name : attr1, attr2, attr3 : method1, method 2

機器將其理解為:

{"class_name" : ['attr1,attr2,attr3', 'method1,method2']}

注意:我稱對象為“Class_Dict”,但稱其為鍵值對更公平。 我想強調的是 - 類 dict 對象應該與容器(也是字典)區分開來,容器可以保存許多鍵值對,每個鍵值對都指定一個要生成的新類。

因此,為了處理繼承,我定義了一個表示它的語法,以及一個解析輸入格式並創建 class_dict 對象的函數。 基本上,它循環遍歷 class_container 字典中的每個鍵值對,如果其中有“>”(指定父子關系),則它會調用該條目上的繼承函數。 繼承函數采用一個條目,該條目詳細說明了繼承層次結構並構建了適當的類。 例如,如果我們有:

classA, classB > classC : A1, A2 / B1, B2 > C1, C2, C3 : Amethod1 / Bmethod1 > Cmethod2

那么 classA 和 classB 是 classC 的父級,所以在機器中:

{"classA, classB > classC" : "A1, A2 / B1, B2 > C1, C2, C3","Amethod / Bmethod > Cmethod"}

變成:

{ClassA : [A1, A2],[Amethod], ClassB : [B1, B2],[Bmethod], ClassC(ClassA, ClassB) : [C1, C2, C3], [Cmethod]}

至少,這就是我正在努力完成的。 我有一些設計問題阻止我以直接的方式實現它。 也就是說,Class_Dict(封裝一個類的規范的對象)是不可迭代的,所以一旦我做了應用繼承規則的轉換,我就不能簡單地做

#psuedocode
del class_container[old entry]
class_container.update(new_entry) #because new_entry is a type Class_Dict, not an iterable

而且我找不到使 Class_Dict 可迭代的方法,因為它是一件事。 我想我可以通過給它iternext方法將它塞進一個迭代中,而不用為它們實現有意義的功能。

還有第二個問題,那就是你不能在循環時調整字典的大小(必須循環以確定在哪些條目上運行繼承函數)。 這對於實現意味着我必須在繼承函數內部有一個單獨的字典,其中包含新生成的 class_dicts,然后返回它並使用返回值更新 class_container。 我遇到的問題是我的代碼在繼承函數中產生了有效的結果(類型為 dict),然后我返回這些結果並得到 None/Nonetype。 我的猜測是因為我用 Class_Dict(a,b,c) 更新了“new”(本地字典)。 repr (),並且這實際上並沒有在內存中構造對象以將其存儲在運行時數據結構中(我對python實現不太了解,只是試了一下)

代碼:

class_container = {}

def from_file(f):
    """
builds a container out of text file
containing class dict specifications.
    """    
    #need to account for lack of .txt in POSIX systems
    with open("{}.txt".format(f), "r") as file:
        for lines in file:
            #if line format OK:
            class_container.update(Class_Dict.to_dict(lines.strip("\n")))
            #else:
            ###throw an error cuz no bueno


def inheritance(name, attr, methods, new = {}):
    family, family_attr, family_methods = name.split(">"), attr.split(">"), methods.split(">")
    #error checking: does the numbers of ops match up. can not proceed if no.
    parents, parent_attr, parent_methods = family[0].split(","), family_attr[0].split("/"), family_methods[0].split("/")
    for a, b, c in zip(parents, parent_attr, parent_methods):
        new.update(Class_Dict(a,b,c).__repr__())
    del family[0], family_attr[0], family_methods[0]
    name, attr, methods = ">".join(family), ">".join(family_attr), ">".join(family_methods)
    if len(family) > 0:
        inheritance(name, attr, methods)
    else:
        return(new)


from_file("classes")
for entries in class_container:
    if entries.count(">") > 0:
        class_container.update(inheritance(entries, class_container[entries][0], class_container[entries][1]))
        #del class_container[entries]
        #class_container.extend(new)

所以我的問題是我的基本案例的主體:

if len(family) > 0:
   inheritance(name, attr, methods)

沒有返回值。 簡單地修改它就解決了這個問題。

if len(family) > 0:it 
   return inheritance(name, attr, methods)

我感興趣的是為什么這可以解決這個問題。 這似乎很矛盾 - 更早返回 None 的事實意味着 else 情況永遠不會被觸發,但它必須為了函數返回所需的值。 使基本情況返回遞歸調用如何產生這種差異?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM