Python code returning error 'Object is not callable'
Question: So, i have started playing around with encryption coding and have been trying
to create a unique encryption key based from time. This is my code so far :

    
    
    from threading import Timer
    from pyclbr import Function
    import hashlib, binascii, time
    
    key1=""
    buffer1=""
    buffer2=""
    
    class RepeatedTimer(object):
        def __init__(self, interval, function, *args, **kwargs):
            self.timer=None
            self.function=function
            self.interval=interval
            self.args=args
            self.kwargs=kwargs
            self.is_running=False
            self.start()
    
        def _run(self):
            self.is_running=False
            self.start()
            self.function(*self.args,**self.kwargs)
    
        def start(self):
            if not self.is_running:
                self.timer=Timer(self.interval,self._run)
                self.timer.start()
                self.is_running=True
    
        def stop(self):
            self.timer.cancel()
            self.is_running=False
    
    def generateKeys():
        global key1
        global buffer1
        global buffer2
    
        t=int((time.time())/10)
        t=hashlib.sha1(str(t).encode('utf-8'))
        t=t.hexdigest()
        t=t.encode('utf-8')
        t=binascii.hexlify(t)
        t=t.decode('utf-8')
        t=t[0:11]
        t=int(t)>>5
        buffer2=buffer1
        buffer1=key1
        key1=str(t)
    
    def printKeys():
        print("Keys : "+key1+" "+buffer1+" "+buffer2)
    
    timerStart=RepeatedTimer(10,generateKeys())
    #generateKeys();
    timerStart2=RepeatedTimer(11,printKeys())
    

Whenever I run the above code I keep getting the error:

    
    
    Exception in thread Thread-49:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 923, in _bootstrap_inner
        self.run()
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 1189, in run
        self.function(*self.args, **self.kwargs)
      File "/Users/Kiro/Documents/LiClipse Workspace/EncryptionFinal/pkg/enc_keygen.py", line 22, in _run
        self.function(*self.args,**self.kwargs)
    TypeError: 'str' object is not callable
    
    Exception in thread Thread-50:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 923, in _bootstrap_inner
        self.run()
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 1189, in run
        self.function(*self.args, **self.kwargs)
      File "/Users/Kiro/Documents/LiClipse Workspace/EncryptionFinal/pkg/enc_keygen.py", line 22, in _run
        self.function(*self.args,**self.kwargs)
    TypeError: 'NoneType' object is not callable
    

Does anyone have any idea why? What I want is the generateKeys() function to
keep running every 10 seconds and the printKeys() function to tell me those
keys every 11 seconds (to check that its assigning the keys correctly to the
global variables).

This is more experimentation than anything but would like help in fixing this
code as i would like to get it working.


Answer: With your line

    
    
    timerStart=RepeatedTimer(10,generateKeys())
    

you are handing over the result of the call `generateKeys()` to the `__init__`
method. What you actually want to do, is to hand over the function itself.
Then you must not call it:

    
    
    timerStart = RepeatedTimer(10, generateKeys)
    

