# Function executing twice

**URL:** <https://discuss.python.org/t/function-executing-twice/40447>\
**Category:** Python Help\
**Created:** [December 6, 2023, 10:10am UTC](https://discuss.python.org/t/function-executing-twice/40447 "2023-12-06T10:10:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anyepeter](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/anyepeter/32/16127_2.png) [@anyepeter](https://discuss.python.org/u/anyepeter)\
**Post date:** [December 6, 2023, 10:10am UTC](https://discuss.python.org/t/function-executing-twice/40447/1 "2023-12-06T10:10:55Z")

</div>

I have a function that takes an array as a parameter and displays just the first value of the array. But in my console, it displays twice. Here is the function

```auto
    def add_annotation(self, data_array, fig):
        
        if fig is not None:
            x_pos = data_array[0]['min'][0]
            y_pos = data_array[0]['min'][2]
            ps = float(data_array[0]['ps'])
            mds = float(data_array[0]['mds'])
            hill = data_array[0]['id']
            annotation_text = f"{hill} <br> PS: {ps:.2f} <br> MDS: {mds:.2f}"
            
            print(x_pos, y_pos)
            fig.add_annotation(
                x=x_pos,
                y=y_pos,
                xref="x",
                yref="y",
                text=annotation_text,
                showarrow=True,
                font=dict(family="Courier New, monospace", size=16, color="#FFFFFF"),
                align="center",
                arrowhead=2,
                arrowsize=1,
                arrowwidth=2,
                arrowcolor="red",
                ax=50,
                ay=-90,
                bgcolor="black",
            )

```

And here is my output  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/4/c/4cd17fae24f4d1ab228fff65fbf627625ef3ac80.png)  
Please any idea of what is happening?

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [December 6, 2023, 11:06am UTC](https://discuss.python.org/t/function-executing-twice/40447/2 "2023-12-06T11:06:41Z")

</div>

You have not provided any of the code that calls that function which is where the problem is I assume.

However I know how you can find out. Using the traceback module you can  
get a stack trace each time your function is called from that you can see  
where in your code the 2 calls come from.

Add the following to add\_annotation.

```auto
def add_annotation(self, data_array, fig):
    import traceback
    traceback.print_stack()

```

---

<div class="post-metadata">

**Author:** ![anyepeter](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/anyepeter/32/16127_2.png) [@anyepeter](https://discuss.python.org/u/anyepeter)\
**Post date:** [December 6, 2023, 11:30am UTC](https://discuss.python.org/t/function-executing-twice/40447/3 "2023-12-06T11:30:54Z")

</div>

Thanks @barry-scott when I use the traceback, here is the output in my console

 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/8/8/88fea86209ed4d088fc70a01b722700f7dc45bbe.png)

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [December 6, 2023, 1:20pm UTC](https://discuss.python.org/t/function-executing-twice/40447/4 "2023-12-06T13:20:05Z")

</div>

Please don’t post screenshots.

The output would be clearer if you wrapped the output of `print_stack()` with some kind of marker:

```auto
print(' ***BEGIN STACK***')
traceback.print_stack()
print(' ***BEGIN STACK***')

```

From the output you provided I can see:

1. `run` calls `process_files` … which calls `add_annotation`.

2. `run` calls `process_folder` which calls `process_files` … which calls `add_annotation`.
