# How to call a function from the logic

**URL:** <https://discourse.slicer.org/t/how-to-call-a-function-from-the-logic/6433>\
**Category:** Support\
**Tags:** python\
**Created:** [April 8, 2019, 3:46pm UTC](https://discourse.slicer.org/t/how-to-call-a-function-from-the-logic/6433 "2019-04-08T15:46:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)\
**Post date:** [April 8, 2019, 3:46pm UTC](https://discourse.slicer.org/t/how-to-call-a-function-from-the-logic/6433/1 "2019-04-08T15:46:03Z")

</div>

Hello,

I was wondering how can I call correctly a function that is in the ModuleNameLogic inherited by ScriptedLoadableModuleLogic in python?  
Currently, I try the following with no success:

```auto
slicer.modules.moduleName.logic().Test()

```

My current code seems like the following and I want to call from Python Interactor the Test() method:

```auto
> class ModuleName(ScriptedLoadableModule):
> def __init__ (self, parent):
> ScriptedLoadableModule. __init__ (self, parent)
> 
> 
> class ModuleNameWidget(ScriptedLoadableModuleWidget):
>   
> def setup(self):
> ScriptedLoadableModuleWidget.setup(self)
> 
> 
> class ModuleNameLogic(ScriptedLoadableModuleLogic):
>   
> def Test(self):
> print("Test is called")
> pass

```

Thanks for your help

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 8, 2019, 8:03pm UTC](https://discourse.slicer.org/t/how-to-call-a-function-from-the-logic/6433/2 "2019-04-08T20:03:35Z")

</div>

Hi @siaeleni -

You should be able to do:

```auto
ModuleNameLogic().Test()

```

if you are in a different module file or in the python interactor you might need to call `import ModuleName` first.

(As an aside, the other ‘logic’ class `slicer.modules.moduleName.logic` is actually the C++ loadable module instance corresponding to the scripted module, but it’s really just an internal structure and doesn’t have any particular utility here).

-Steve

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 8, 2019, 8:29pm UTC](https://discourse.slicer.org/t/how-to-call-a-function-from-the-logic/6433/3 "2019-04-08T20:29:30Z")

</div>

I usually create a single logic instance in the widget that I can access via the widget class:

```
slicer.modules.screencapture.widgetRepresentation().self().logic

```

Should we update updating the scripted module template to create a logic instance in the widget’s constructor instead of creating a new logic instance each time? This would make the Python and C++ modules a bit more consistent.

---

<div class="post-metadata">

**Author:** ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)\
**Post date:** [April 9, 2019, 3:15pm UTC](https://discourse.slicer.org/t/how-to-call-a-function-from-the-logic/6433/4 "2019-04-09T15:15:56Z")

</div>

I created an instance and use that to access to the logic methods.Thanks a lot!
