Clock unschedule by return False with help of decorator

16 views
Skip to first unread message

Himanshu Pharawal

unread,
Jan 22, 2018, 2:51:59 AM1/22/18
to Kivy users support
There is decorator:

def counter(func):
   
def wrapper(*args, **kargs):
        wrapper
.count += 1
       
return func(*args, **kargs)
    wrapper
.count = 0
   
return wrapper

Now here is kivy code:

class Controller(Widget):

   
@counter
   
def my_callback(self, dtt):
       
self.ids.label1.text = 'My callback is called! after' + str(dtt)
       
if self.my_callback.count == 10:
           
return False

   
def on_touch_down(self, touch):
       
Clock.schedule_interval(self.my_callback, 1 / 5)

It gives en error:

AttributeError: 'Controller' object has no attribute 'wrapper'

But with simple class it works:



ZenCODE

unread,
Jan 25, 2018, 3:58:54 PM1/25/18
to Kivy users support
Whatever it is you're trying to do, I'm sure there is a better way. A decorator is not really made to track state between different calls, especially calls to different object instances. It's hard to say exactly why it breaks because
 
a. You don't give the complete error. What line? what context?
b. The code above is not runnable and only shows isolated parts.

If you can post a runnable example, that would help. But please also state what you are trying to do. I'm sure there is an easier, more readable way to do it.

Cheers 
Reply all
Reply to author
Forward
0 new messages