Hi Tom!
That’s a good question! There’s a few key things to remember here - and forgive me if I’m being overly simple. There are many here that are new to Python in general.
-
self.run_in
is a function
-
self.run_in(self.light_off, 10)
will call the function and return a result
-
self
is just a silly name we use to say “this instance”, you could honestly say replace it with any other valid python variable name.
So, what’s really going on when you call self.x = self.run_in(self.light_off, 10)
?
Python goes out and executes the code under run_in
with the params you gave it and returns an id, which you can cancel the event from. This is what self.x
is. If you were put log self.x
in the line right after, you’d see exactly that, it’s just an id code! If you were to do the same line twice in a row…
self.x = self.run_in(self.light_off, 10)
self.x = self.run_in(self.light_off, 11)
then all you’re going to end up doing is overwriting self.x
with the second id. x
is just a name/reference to some string (the handle id) that’s returned by the call to run_in
. Since it’s just a name, you can redefine it as many times as you want… but you’ll lose the old id!
Now, remember what I said about self
? It’s a name that just means “this instance”. So if you’re going to only have 1 of these per [instance of that] app, then you’ll be totally fine calling it self.x
wherever you go.
Toy examples below!
Bad.
# hellov1.py
class HelloWorld:
def initialize(self):
self.x = self.run_in(self.light_off, 10)
self.log(self.x) # == '1a88576f-be12-473b-8dfa-3caf124ed526'
self.run_in(self.do_stuff, 3)
def do_stuff(self, kwargs):
self.x = self.run_in(self.light_off, 7)
self.log(self.x) # == '691d2f9c-119c-49fe-84ca-bcc6bfe51ca9'
def light_off(self, kwargs):
...
Good.
# hellov2.py
class HelloWorld:
def initialize(self):
self.x = self.run_in(self.light_off, 10)
self.log(self.x) # == '1a88576f-be12-473b-8dfa-3caf124ed526'
def do_stuff(self, kwargs):
# do cool stuff here
self.log(self.x) # == '1a88576f-be12-473b-8dfa-3caf124ed526'
def light_off(self, kwargs):
...
class GoodbyeWorld:
def initialize(self):
self.x = self.run_in(self.light_off, 10)
self.hello_world_app = self.get_app('hello_world')
self.run_in(self.do_other_stuff, 3)
def do_other_stuff(self, kwargs):
self.log(self.x) # == '29859334-140f-4003-8e10-87a041c47d9c'
self.log(self.hello_world_app.x) # == '1a88576f-be12-473b-8dfa-3caf124ed526'
def light_off(self, kwargs):
...
Hope this helps!
- SN