Page 1 of 1

OnTick listener as a class method

Posted: Thu May 05, 2016 6:53 pm
by BackRaw
Hi,

just wanted to ask whether it's a good or bad idea to have a class method as an OnTick listener and why.

Thanks :D

Re: OnTick listener as a class method

Posted: Fri May 06, 2016 12:05 am
by satoon101
I guess it really doesn't matter. It will actually act as a staticmethod (regular function), anyway. Meaning, it will not have access to the class or the instance (if you make an instance of one). Now, if you use on_tick_listener_manager.register_listener on a method of the class/instance, it will have access to the specific object. Just test out this example:

Syntax: Select all

from listeners import OnTick
from listeners import on_tick_listener_manager

class Test(object):
@classmethod
def test_one(cls, *args):
print('test_one', cls, args)

@OnTick
def test_two(*args):
print('test_two', args)

def test_three(self, *args):
print('test_three', self, args)

var = Test()

on_tick_listener_manager.register_listener(Test.test_one)
on_tick_listener_manager.register_listener(var.test_three)

def unload():
on_tick_listener_manager.unregister_listener(Test.test_one)
on_tick_listener_manager.unregister_listener(var.test_three)

Re: OnTick listener as a class method

Posted: Fri May 06, 2016 2:24 am
by BackRaw
Great, thanks!