Decreasing the number of arguments in a function in Python?

My question is how to deal with the piece of code where I use Caesar's cipher .

The Decrypt and Encrypt functions must deal with the limits of the alphabet (A - Z and a - z) . I tried to write two possible loops for both alphabets in one cyclic function called cycencrypt .

But the function takes 6 arguments , and I read somewhere that is less readable and understandable, having more than 3 arguments in one function, so my question is:

Should I reduce the number of arguments by separating the two functions and bring the code snippet longer (but perhaps more understandable)? Thanks for any answer, I approve of this.

EDIT: Docstrings around functions have been removed to make the main goal of my question visible.

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)         

Loop for encrypting with arguments and negative offset control

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))

the main part of the encryption takes a lot of arguments in two lines with print

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)
+4
source share
3 answers

In such a situation, it is often better to write your code as a class . The class constructor can only accept the minimum number of required arguments (which can be nothing at all!), And then optional arguments can be set as properties of the class or using other methods.

, , , , .

, , :

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

, , Cypher, , !

encrypt decrypt, . , ​​, :

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

datetime.datetime:

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().

+2

, (, - , , , , ).

, , docstrings.

, (, 1 0).

In any case, for a long line, use the PEP8 recommendations that describe how to correctly jump lines (thirst lines do not exceed 80 characters, according to PEP8 ).

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)    
0
source

Source: https://habr.com/ru/post/1688682/


All Articles