1

How do you pass arguments between defs within a class using the timer object?

def onrepeat(self,user, arg):
    args = arg.split()
    messagearg = args[0]
    colorarg = args[1]
    if colorarg == "black":
        color = "&0"
    if colorarg == "dark blue" or "darkblue":
        color = "&1"
    if colorarg == "dark green" or "darkgreen":
        color = "&2"
    if colorarg == "dark teal" or "darkteal":
        color = "&3"
    if colorarg == "dark red" or "darkred":
        color = "&4"
    if colorarg == "purple":
        color = "&5"
    if colorarg == "gold":
        color = "&6"
    if colorarg == "gray":
        color = "&7"
    if colorarg == "dark gray" or "darkgrey" or "dark grey" or "darkgrey":
        color = "&8"
    if colorarg == "blue":
        color = "&9"
    if colorarg == "bright green" or "brightgreen":
        color = "&a"
    if colorarg == "teal":
        color = "&b"
    if colorarg == "red":
        color = "&c"
    if colorarg == "pink":
        color = "&d"
    if colorarg == "yellow":
        color = "&e"
    if colorarg == "white":
        color = "&f"
    if colorarg == "":
        color = "&f"
    self.timerobj = Timer(3.0, self.repeat2)
    self.timerobj.start()

def repeat2(self,messagearg,color):
    self.bot.sendMessage(color + messagearg)

This will give me an error saying:

repeat2 needs 3 arguments while only 2 are given.

To fix this I change

self.timerobj = Timer(3.0, self.repeat2) 

to

self.timerobj = Timer(3.0, self.repeat2(messagearg,color))

but that doesn't work either!

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
joseph
  • 1,021
  • 2
  • 9
  • 11
  • As an aside, that sequence of `if` statements is **hideous**. Use a [`dict`](http://www.python.org/doc//current/library/stdtypes.html#dict) for pity's sake (or remove it, since `color` is never used in your example)! – johnsyweb Apr 16 '11 at 00:12
  • color is used in the self.bot.sendMessage argument – joseph Apr 16 '11 at 00:16
  • It is not used within the context of `onrepeat()`. Are you saying it is `global`? – johnsyweb Apr 16 '11 at 00:22
  • no, color is used in the second def repeat2 (at the bottom of the code) because the timer thread requires that you use a second def as of my knowing in order to have a wait in time before executing code. I am sorry if i am not perfectly clear because i am new to programming. – joseph Apr 16 '11 at 00:27
  • 2
    BTW, your conditions won't evaluate the way you think. This: `colorarg == "dark blue" or "darkblue"` will always evaluate True. – Keith Apr 16 '11 at 00:39

1 Answers1

0

Timer has the following signature

class threading.Timer(interval, function, args=[], kwargs={})

Note that it accepts args and kwargs. So you send your args to Timer function by just mentioning them. Try this:

Timer(3.0, self.repeat2,messagearg,color)
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131