Dynamically declared shared constant/variable imported twice problem

Hello everyone,

I have three modules A, B, C;

A declares this globally :

  UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent()

Then they import stuff from each other:

- A imports a constant from module B (in "__main__")
- A imports a class and some constants from module C
- B imports a constant from module A
- C imports UPDATE_EVENT_ID from module A

What happens is that since A imports stuff from B which then in turn
loads something from A, UPDATE_EVENT_ID gets redefined and when A sends
an event to C, C doesn't do anything since it doesn't have the same
UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had
been a statically defined variable then that wouldn't be "bad", but since it's dynamic it does pose problems. More globally, how cn I prevent this even with static constants? I don't think it's that great
to redefine the stuff multiple times and I've already had this problem in the past with a constant dict.

Thank you,
Gabriel

Sorry for re-posting it here, it was ment for another list

Gabriel Rossetti wrote:

···

Hello everyone,

I have three modules A, B, C;

A declares this globally :

UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent()

Then they import stuff from each other:

- A imports a constant from module B (in "__main__")
- A imports a class and some constants from module C
- B imports a constant from module A
- C imports UPDATE_EVENT_ID from module A

What happens is that since A imports stuff from B which then in turn
loads something from A, UPDATE_EVENT_ID gets redefined and when A sends
an event to C, C doesn't do anything since it doesn't have the same
UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had
been a statically defined variable then that wouldn't be "bad", but since it's dynamic it does pose problems. More globally, how cn I prevent this even with static constants? I don't think it's that great
to redefine the stuff multiple times and I've already had this problem in the past with a constant dict.

Thank you,
Gabriel
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

I like to put static constants in one place, such as a modConstant module, and grab them from there. If you need a local copy of an object, you need to use a copy or deep copy, otherwise you just copy the reference, which simply points back to the original object:

import copy

a = ‘something’

b= copy.copy(a)

c = copy.deepcopy(a)

Copy makes a copy at the first level, deep copy goes down through the structure. For example, with a list, copy makes a new list, but each list element is the same. You could then append to the list without changing the first list, but if you change any of the elements it changes them in both places. Deep copy avoids this by making a new list and new elements.

Tom

···

On Fri, 2009-05-01 at 12:15 +0200, Gabriel Rossetti wrote:


Hello everyone,
I have three modules A, B, C;
A declares this globally :
UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent()
Then they import stuff from each other:
- A imports a constant from module B (in "__main__")
- A imports a class and some constants from module C
- B imports a constant from module A
- C imports UPDATE_EVENT_ID from module A
What happens is that since A imports stuff from B which then in turn
loads something from A, UPDATE_EVENT_ID gets redefined and when A sends
an event to C, C doesn't do anything since it doesn't have the same
UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had
been a statically defined variable then that wouldn't be "bad", but since it's dynamic it does pose problems. More globally, how cn I prevent this even with static constants? I don't think it's that great
to redefine the stuff multiple times and I've already had this problem in the past with a constant dict.
Thank you,
Gabriel
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
[http://lists.wxwidgets.org/mailman/listinfo/wxpython-users](http://lists.wxwidgets.org/mailman/listinfo/wxpython-users)