簡體   English   中英

減少Python中函數的參數數量?

[英]Reducing the number of arguments in function in Python?

我的問題是如何處理我使用凱撒密碼的代碼片段

函數Decrypt和Encrypt必須處理字母表的限制(A - Z和a - z) 我試圖在一個名為cycleencrypt的循環函數中為兩個字母編寫兩個可能的循環。

但是該函數需要大約6個參數,而且我已經閱讀了一個在一個函數中具有3個以上參數的可讀性和可理解性較低的地方,所以我的問題是:

我應該通過拆分兩個函數來減少參數的數量,並使代碼片段更長(但可能更容易理解)? 謝謝你的回答,我很感激。

編輯:刪除了函數周圍的文檔字符串,以使我的問題的主要目的可見。

def offsetctrl(offset):
    while offset < 0:
        offset += 26
    return offset

def cycleencrypt(string, offset, index, listing, first, last):
    offset = offsetctrl(offset)
    if string >= ord(first) and string <= ord(last):
        string += offset
        while string > ord(last):
            string = ord(first) + (string - ord(last) -1)
        listing[index] = chr(string)         

循環加密有大量參數和負偏移控制

def encrypt(retezec, offset):
    listing = list(retezec)
    for index in range(0, len(retezec)):
        string = ord(retezec[index])
        cycleencrypt(string, offset, index, listing, 'A', 'Z')
        cycleencrypt(string, offset, index, listing, 'a', 'z')
    print(''.join(listing))

主加密部分采用兩行打印多個參數

def decrypt(retezec, offset):
    return encrypt(retezec, -offset)

if __name__ == "__main__":
encrypt("hey fellow how is it going", 5)
decrypt("mjd kjqqtb mtb nx ny ltnsl", 5)

在這種情況下,將代碼編寫為通常會更好。 您的類的構造函數可以只使用所需的最少數量的參數(可能根本不需要 !),然后可以將可選參數設置為類的屬性或使用其他方法。

在設計這樣的類時,我發現最先寫入客戶端代碼是最有用的 - 也就是說,編寫將首先使用該類的代碼,然后從那里開始設計該類。

例如,我可能希望代碼看起來像這樣:

cypher = Cypher()
cypher.offset = 17
cypher.set_alphabet('A', 'Z')
result = cypher.encrypt('hey fellow how is it going')

希望應該清楚如何從這里開始工作到Cypher類的設計,但如果沒有,請在Stack Overflow上提出一個問題!

如果您想提供encryptdecrypt方便的方法,它仍然很容易做到。 例如,您可以編寫如下函數:

def encrypt(text, offset):
    cypher = Cypher()
    cypher.offset = offset
    return cypher.encrypt(text)

這是datetime.datetime的docstring:

class datetime(date):
    """datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
    ...
    """

以及它的構造函數的簽名:

def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0, microsecond=0, tzinfo=None):

我們可以從中學到什么:

  • 添加完全有意義的參數
  • 使用參數並為參數提供合理的默認值

一邊想:你認為圖書館的用戶應該使用cycleencrypt()嗎? 你可以將它標記為私有(帶下划線),這樣每個人都會看到它不是公共API,而是應該使用encrypt()decrypt()代替。

只要不存在十幾個參數,參數的數量就不重要了(也許有人可以鏈接到你提到的關於超過3個參數的內容,我可能是錯的)。

為了在函數定義中更具可讀性,請按照docstrings約定編寫注釋。

為了在函數調用時更具可讀性,請在定義中盡可能為更有用的值提供默認值(例如,offset可以默認值為1,索引0)。

無論哪種方式,對於長線,使用PEP8指南描述了正確跳線的方法(根據PEP8 ,線不得超過80個字符)。

def cycleencrypt(string, offset=1, index=0,
                     listing, first, last):
        """Description

        :param string: description
        :param offset: description
        :param index: description
        :param listing: description
        :param first: description
        :param last: description
        :return description
        """
        offset = offsetctrl(offset)
        if string >= ord(first) and string <= ord(last):
            string += offset
            while string > ord(last):
                string = ord(first) + (string - ord(last) - 1)
            listing[index] = chr(string)    

暫無
暫無

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

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