# Slicer script repository: module\_name.logic vs slicer.module\_name.widgetRepresentation().self().logic() vs slicer.modules.logic()

**URL:** https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454
**Category:** Support
**Tags:** python
**Created:** [September 12, 2025, 3:42am UTC](https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454 "2025-09-12T03:42:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tas47](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tas47/32/78529_2.png) [@tas47](https://discourse.slicer.org/u/tas47)
#### Post date: [September 12, 2025, 3:42am UTC](https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454/1 "2025-09-12T03:42:53Z")

</div>

Slicer API explanation:

Hello all I have been trying to understand the Slicer API and I am having a lot of trouble understanding exactly how the ecosystem works.

I want to specifically understand how the logic aspect of modules works in Slicer.  
In the script repository, I have seen the logic() incorporated in 3 different ways:

Example 1:  
SegmentStatistics is often imported and accessed like this:

```auto
import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic().Compute

```

However, I have seen other modules logic method accessed this way:  
Example 2:

```auto
slicer.modules.markups.logic().ExportControlPointsToCSV(markupsNode, "/path/to/MyControlPoints.csv")

```

so how come I cannot access ComputeStatistics() like this with the code below?

```auto
slicer.modules.segmentstatistics.logic()

```

I can also see that i can access ComputeStatistics() if I access it via widgetRepresentation  
Example 3:

```auto
slicer.modules.segmentstatistics.widgetRepresentation().self().logic.computeStatistics()

```

Overall what is the difference between the 3 ways? Which one the correct way and the theory behind the code. I understand that widgetRepresentation handles the gui and by qt, but I cant wrap my head behind Slicer design pattern especially with all the vtk/qt bindings.  
Finally, whats the intuition behind accessing the self() in widget-representation from Slicer? Are the modules not already instantiated when slicer is loaded? I have seen this often used with segment editor example to access the different modes like pen, eraser and etc.

Thank you  
Tas

---

<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: [September 13, 2025, 3:14pm UTC](https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454/2 "2025-09-13T15:14:52Z")

</div>

Good question! To facilitate dynamic reloading, we don’t use `slicer.modules.segmentstatistics.logic()` in scripted modules. Instead, we usually let the widget create and own the logic in Python scripted modules. See more information in slide 11 in the [STC-DEV-101: Slicer Scripting and Module Development](https://training.slicer.org/#STC-DEV-101) tutorial.

---

<div class="post-metadata">

### Author: ![tas47](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tas47/32/78529_2.png) [@tas47](https://discourse.slicer.org/u/tas47)
#### Post date: [September 26, 2025, 2:22pm UTC](https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454/3 "2025-09-26T14:22:53Z")

</div>

Thank you @Iassoan  
Just to make sure, if i want to access all the attributes and instance of logic modules, you always recommend accessing from the widget itself via `WidgetRepresenation()` instead of accessing it with `slicer.modules.segmentstatistics` ?

---

<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: [September 26, 2025, 7:10pm UTC](https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454/4 "2025-09-26T19:10:06Z")

</div>

For a scripted module, you cannot reach module logic via the module object directly like `slicer.modules.segmentstatistics.logic()`, as this returns a placeholder object, not the real module logic object (that the widget instantiates and owns). This may change in the future, so the safest and most future-proof method is to use `slicer.util.getModuleLogic()` function.

---

<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: [September 26, 2025, 7:30pm UTC](https://discourse.slicer.org/t/slicer-script-repository-module-name-logic-vs-slicer-module-name-widgetrepresentation-self-logic-vs-slicer-modules-logic/44454/5 "2025-09-26T19:30:46Z")

</div>

A small addition: `segStatLogic = SegmentStatistics.SegmentStatisticsLogic()` instantiates a new logic class. It does not access the logic class of the built-in module. This type of access is useful if you just want a temporary logic class that you use for some computations without affecting the state of the “Segment Statistics” module. You need to ensure that the logic is properly initialized. In many cases, no initialization is needed, but you can double-check by looking at how the module widget class does it.
