__init__ called twice if object constructed in alert

What I’m trying to do:
I added an alert in a class init method and noticed it being raised twice if my object was constructed within the alert. I might be explaining things poorly , so lets just look at the code!

The following button clicks yield different results:

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    alert(content=Form2())

  def button_2_click(self, **event_args):
    """This method is called when the button is clicked"""
    alert_form = Form2()
    alert(alert_form)

button_1_click will fire the init method of Form2 twice and button_2_click will call the init method once.

Clone link:
share a copy of your app

EDIT 11/9/22

I found out this also is accuring in the following situation, which is the same nature as above, but still was unexcepted to me:

def get_details():
   print("THIS MESSAGE WILL PRINT TWICE")

anvil.server.call('<server_call>',**get_details())

It looks like you found a bug of Skulpt: named arguments are executed twice.

I have created a module that defines a class and a function:

def f(c):
    print(c)

class C:
    def __init__(self):
        print('hello')

Then I have called it with and without named argument:

        print('calling f without named argument')
        f(C())

        print('calling f with named argument')
        f(c=C())

The result is double execution with named argument.

It looks like this has nothing to do with forms. Can be reproduced also from the console:
image

2 Likes

Thank you for the clarity!

EDIT: submitting an issue to the github repo. #1470

1 Like

How odd - moved to bug reports and will look into it.

1 Like

Bug identified and a fix will be shipped soon.
Thanks @anthonys for also reporting on the skulpt repo.
We’ve PRed the fix back to the project.

1 Like

Cool–Thanks! I just found the same issue causing my forms to take twice as long to init and came to find anything similar to fix what I might be doing wrong.

1 Like

Offhand, until this is fixed, does anyone know which is happening?

  1. __init__ called twice on the same object
  2. __init__ called once each on two different objects

In the first case, code inside __init__ can tell the difference, and compensate for the effect. In the second, it can’t.

Try using print( id(self) ) inside the init and see if the objects id’s are the same.

Wait, would that work in skulpt? I know skuplt had to implement id differently in JS than in cPython…

This yielded two different ID’s but I also do not know if that proves they are two different objects.

After trying to look into it as far as I can tell, if they have different ID’s they are different objects. Javascript does not allow skulpt to gain access to the memory address of the object like in normal python, so when an object is created it gets assigned an id that is pretty much just an integer that counts up from 0.

So the first object ID skulpt makes is 0, then the next is 1, 2 , 3 etc.

If the results of id() do not match they are not the same object in Javascript, skulpt, or python, since the id seems to just be assigned by some global id value that increments by one at each creation of a new object.

Which makes me wonder, if your skulpt code sits there churning out new, then destroying objects all day 24/7 will it eventually all go sideways if it creates more than 2147483647 objects.
:thinking: