[Updated script to use repr(mainFunc), which shows that the location of mainFunc() changes from the first definition to the second.]
Yes, in this case all rules within your script share the same scope, as do the functions they wrap. The redefinition of the object known as mainFunction
replaces the first definition, but the code from the first definition is not replaced, only the association of the global name mainFunction
with that code is lost. After the second definition, the name mainFunction
becomes associated with the code found in the body of the second definition. That’s not un-pythonic, that’s just how scope works in python. Here’s a simple script with its output that I hope illustrates what I’m trying to describe:
from org.slf4j import Logger, LoggerFactory
from openhab.rules import rule
from openhab.triggers import when
logger = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.jsr223")
@rule("Rule 1")
@when("Member of gScene_Monitor_Scene_01 changed")
def mainFunction(event):
logger.info("Rule 1 triggered by: %s" % event.itemName)
logger.info("1st mainFunction def, objects at local scope: %s" % ", ".join(locals()))
logger.info("1st mainFunction def, objects at global scope: %s" % ", ".join(globals()))
logger.info("mainFunc: %s" % repr(mainFunction))
logger.info("objects at global scope: %s" % ", ".join(globals()))
@rule("Rule 2")
@when("Member of gScene_Monitor_Scene_02 changed")
def mainFunction(event):
logger.info("Rule 2 triggered by: %s" % event.itemName)
logger.info("2nd mainFunction def, objects at local scope: %s" % ", ".join(locals()))
logger.info("2nd mainFunction def, objects at global scope: %s" % ", ".join(globals()))
logger.info("mainFunc: %s" % repr(mainFunction))
logger.info("objects at global scope: %s" % ", ".join(globals()))
2018-11-17 07:53:09.534 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'explore-scope-vars.py'
2018-11-17 07:53:09.743 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: target=[Member of gScene_Monitor_Scene_01 changed], target_type=Member of, trigger_target=gScene_Monitor_Scene_01, trigger_type=changed, old_state=None, new_state=None
2018-11-17 07:53:09.759 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: Created item_trigger: [Item-OfcTblLamp-changed]
2018-11-17 07:53:09.763 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: Created item_trigger: [Item-OfficeFanLts-changed]
2018-11-17 07:53:09.766 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: Created item_trigger: [Item-TstLamp1-changed]
2018-11-17 07:53:09.769 [DEBUG] [omation.core.internal.RuleEngineImpl] - _FunctionRule: self.name:
2018-11-17 07:53:09.784 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={service.id=664, service.bundleid=227, service.scope=singleton, event.topics=smarthome/items/OfcTblLamp/*} - org.eclipse.smarthome.automation.module.core
2018-11-17 07:53:09.796 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={service.id=665, service.bundleid=227, service.scope=singleton, event.topics=smarthome/items/OfficeFanLts/*} - org.eclipse.smarthome.automation.module.core
2018-11-17 07:53:09.807 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={service.id=666, service.bundleid=227, service.scope=singleton, event.topics=smarthome/items/TstLamp1/*} - org.eclipse.smarthome.automation.module.core
2018-11-17 07:53:09.815 [INFO ] [clipse.smarthome.model.script.jsr223] - mainFunc: <function mainFunction at 0x6>
2018-11-17 07:53:09.831 [INFO ] [clipse.smarthome.model.script.jsr223] - objects at global scope: Action, ActionBuilder, CLOSED, Command, Condition, ConditionBuilder, Configuration, DECREASE, DOWN, DateTimeType, DecimalType, FASTFORWARD, File, FileUtils, FilenameUtils, HSBType, INCREASE, ImperialUnits, IncreaseDecreaseType, Logger, LoggerFactory, MOVE, MetricPrefix, ModuleBuilder, NEXT, NULL, NextPreviousType, OFF, ON, OPEN, OnOffType, OpenClosedType, PAUSE, PLAY, PREVIOUS, PercentType, PlayPauseType, PointType, QuantityType, REWIND, RawType, RewindFastforwardType, Rule, SIUnits, STOP, SmartHomeUnits, State, StopMoveType, StringListType, StringType, StringUtils, Trigger, TriggerBuilder, UNDEF, UP, URLEncoder, UnDefType, UpDownType, __builtins__, audio, automationManager, events, ir, itemRegistry, items, logger, mainFunction, rule, ruleRegistry, rules, scriptExtension, se, things, voice, when
2018-11-17 07:53:09.838 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: target=[Member of gScene_Monitor_Scene_02 changed], target_type=Member of, trigger_target=gScene_Monitor_Scene_02, trigger_type=changed, old_state=None, new_state=None
2018-11-17 07:53:09.842 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: Created item_trigger: [Item-OfcTblLamp-changed]
2018-11-17 07:53:09.847 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: Created item_trigger: [Item-OfficeFanLts-changed]
2018-11-17 07:53:09.851 [DEBUG] [omation.core.internal.RuleEngineImpl] - @when: Created item_trigger: [Item-TstLamp1-changed]
2018-11-17 07:53:09.854 [DEBUG] [omation.core.internal.RuleEngineImpl] - _FunctionRule: self.name:
2018-11-17 07:53:09.866 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={service.id=667, service.bundleid=227, service.scope=singleton, event.topics=smarthome/items/OfcTblLamp/*} - org.eclipse.smarthome.automation.module.core
2018-11-17 07:53:09.877 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={service.id=668, service.bundleid=227, service.scope=singleton, event.topics=smarthome/items/OfficeFanLts/*} - org.eclipse.smarthome.automation.module.core
2018-11-17 07:53:09.888 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={service.id=669, service.bundleid=227, service.scope=singleton, event.topics=smarthome/items/TstLamp1/*} - org.eclipse.smarthome.automation.module.core
2018-11-17 07:53:09.898 [INFO ] [clipse.smarthome.model.script.jsr223] - mainFunc: <function mainFunction at 0x7>
2018-11-17 07:53:09.904 [INFO ] [clipse.smarthome.model.script.jsr223] - objects at global scope: Action, ActionBuilder, CLOSED, Command, Condition, ConditionBuilder, Configuration, DECREASE, DOWN, DateTimeType, DecimalType, FASTFORWARD, File, FileUtils, FilenameUtils, HSBType, INCREASE, ImperialUnits, IncreaseDecreaseType, Logger, LoggerFactory, MOVE, MetricPrefix, ModuleBuilder, NEXT, NULL, NextPreviousType, OFF, ON, OPEN, OnOffType, OpenClosedType, PAUSE, PLAY, PREVIOUS, PercentType, PlayPauseType, PointType, QuantityType, REWIND, RawType, RewindFastforwardType, Rule, SIUnits, STOP, SmartHomeUnits, State, StopMoveType, StringListType, StringType, StringUtils, Trigger, TriggerBuilder, UNDEF, UP, URLEncoder, UnDefType, UpDownType, __builtins__, audio, automationManager, events, ir, itemRegistry, items, logger, mainFunction, rule, ruleRegistry, rules, scriptExtension, se, things, voice, when
2018-11-17 07:53:09.907 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: explore-scope-vars.py
2018-11-17 07:53:20.888 [DEBUG] [omation.core.internal.RuleEngineImpl] - The trigger 'Item-TstLamp1-received-update' of rule 'e8b03910-e9fa-4d55-8814-d50fe90c5421' is triggered.
2018-11-17 07:53:20.979 [DEBUG] [omation.core.internal.RuleEngineImpl] - The rule 'e8b03910-e9fa-4d55-8814-d50fe90c5421' is executed.
2018-11-17 07:53:21.004 [DEBUG] [omation.core.internal.RuleEngineImpl] - The trigger 'Item-TstLamp1-changed' of rule '82dbd384-3348-4397-9ac0-94c1814546c5' is triggered.
2018-11-17 07:53:21.007 [DEBUG] [omation.core.internal.RuleEngineImpl] - The trigger 'Item-TstLamp1-changed' of rule 'cd0c197d-ca04-445e-97f6-fdbb8ecbb5dc' is triggered.
2018-11-17 07:53:21.014 [INFO ] [clipse.smarthome.model.script.jsr223] - Rule 1 triggered by: TstLamp1
2018-11-17 07:53:21.014 [INFO ] [clipse.smarthome.model.script.jsr223] - Rule 2 triggered by: TstLamp1
2018-11-17 07:53:21.027 [INFO ] [clipse.smarthome.model.script.jsr223] - 1st mainFunction def, objects at local scope: event
2018-11-17 07:53:21.027 [INFO ] [clipse.smarthome.model.script.jsr223] - 2nd mainFunction def, objects at local scope: event
2018-11-17 07:53:21.034 [INFO ] [clipse.smarthome.model.script.jsr223] - 1st mainFunction def, objects at global scope: Action, ActionBuilder, CLOSED, Command, Condition, ConditionBuilder, Configuration, DECREASE, DOWN, DateTimeType, DecimalType, FASTFORWARD, File, FileUtils, FilenameUtils, HSBType, INCREASE, ImperialUnits, IncreaseDecreaseType, Logger, LoggerFactory, MOVE, MetricPrefix, ModuleBuilder, NEXT, NULL, NextPreviousType, OFF, ON, OPEN, OnOffType, OpenClosedType, PAUSE, PLAY, PREVIOUS, PercentType, PlayPauseType, PointType, QuantityType, REWIND, RawType, RewindFastforwardType, Rule, SIUnits, STOP, SmartHomeUnits, State, StopMoveType, StringListType, StringType, StringUtils, Trigger, TriggerBuilder, UNDEF, UP, URLEncoder, UnDefType, UpDownType, __builtins__, audio, automationManager, events, ir, itemRegistry, items, logger, mainFunction, rule, ruleRegistry, rules, scriptExtension, se, things, voice, when
2018-11-17 07:53:21.036 [DEBUG] [omation.core.internal.RuleEngineImpl] - The rule '82dbd384-3348-4397-9ac0-94c1814546c5' is executed.
2018-11-17 07:53:21.053 [INFO ] [clipse.smarthome.model.script.jsr223] - 2nd mainFunction def, objects at global scope: Action, ActionBuilder, CLOSED, Command, Condition, ConditionBuilder, Configuration, DECREASE, DOWN, DateTimeType, DecimalType, FASTFORWARD, File, FileUtils, FilenameUtils, HSBType, INCREASE, ImperialUnits, IncreaseDecreaseType, Logger, LoggerFactory, MOVE, MetricPrefix, ModuleBuilder, NEXT, NULL, NextPreviousType, OFF, ON, OPEN, OnOffType, OpenClosedType, PAUSE, PLAY, PREVIOUS, PercentType, PlayPauseType, PointType, QuantityType, REWIND, RawType, RewindFastforwardType, Rule, SIUnits, STOP, SmartHomeUnits, State, StopMoveType, StringListType, StringType, StringUtils, Trigger, TriggerBuilder, UNDEF, UP, URLEncoder, UnDefType, UpDownType, __builtins__, audio, automationManager, events, ir, itemRegistry, items, logger, mainFunction, rule, ruleRegistry, rules, scriptExtension, se, things, voice, when
2018-11-17 07:53:21.058 [DEBUG] [omation.core.internal.RuleEngineImpl] - The rule 'cd0c197d-ca04-445e-97f6-fdbb8ecbb5dc' is executed.
2018-11-17 07:53:23.710 [DEBUG] [omation.core.internal.RuleEngineImpl] - The trigger 'Item-TstLamp1-received-update' of rule 'e8b03910-e9fa-4d55-8814-d50fe90c5421' is triggered.
2018-11-17 07:53:23.717 [DEBUG] [omation.core.internal.RuleEngineImpl] - The rule 'e8b03910-e9fa-4d55-8814-d50fe90c5421' is executed.
Note that in the logs of globals, there is only one entry for mainFunction
.