0

I recently worked on a simple and basic encryption program. (skip reading the code before reading the question)

def generator():
    enckey="abcdefghijklmnopqrstuvwxyz"
    a=list(enckey)
    random.shuffle(a)
    c=''
    for i in a:
        c+=i
    c=c+a[0]
    return c


def senc(a,enckey=generator()):
    b=list(a)
    for i in range(0,len(a)):
        for j in range(0,27):
            if b[i]==enckey[j]:
                b[i]=enckey[j+1]
                break
    c=''
    for i in b:
        c+=i
    print("Encrypted text:",c,"\nEncryption Key:",enckey)

Now when I call the function senc("argument"), it generates a enckey and uses it to encrypt the supplied text. Now, since I say that enckey=generator() I expect that every time senc is executed it will re execute generator to get a key. But this did not happen.

The following is the proof:

>>> id(senc("Andy"))
Encrypted text: Arnj 
Encryption Key: htpkzwqlxcsdnregobvaimyjufh
1655676136
>>> id(senc("Candy"))
Encrypted text: Cirnj 
Encryption Key: htpkzwqlxcsdnregobvaimyjufh
1655676136

Both are at the same memory address and as seen the encryption key is same. Where did I go wrong, and why does it not call the function?

Please note that the encryption key does change for every new instance of IDLE/commandprompt, etc.

2
  • 2
    The default value is evaluated when the function is defined, not when the function is called. Commented Nov 2, 2020 at 16:25
  • 1
    See Common Gotchas. Commented Nov 2, 2020 at 16:25

1 Answer 1

2

Default arguments are evaluated only once, when the function gets defined. If you want to have a new generator each time you invoke the function without enckey parameter, you should do:

def senc(a,enckey=None):
    if enckey is None:
        enckey = generator()
    b=list(a)
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

this implies that it remembers the default value, can I get it's memory address/clear it from there?
@Barmar Thanks a lot :)
It also seems to be writable, see stackoverflow.com/questions/13784840/…. But that would be an ugly and difficult to understand way to do this, while using None as a default is the commonly understood, traditional way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.