Sunday 15 June 2014

Python defines my class twice on the same thread -


I assumed that if I had something in Python

  class A: print (" Hi ")   

If it is explicitly removed in some del A

then it is" Hi " Only a single time will be printed on my little project I have this code (name is changed):

  class A (isom): print (threading curant_ thread) () try: AMYDTT # Yes, it should never give results But also in an error, but this code should be twice except print (AMIDICT) nominal: print ("A.MY_DICT unknown") MY_DICT = {}   

And it produces this output:

  $ python main.py   

So the same class level code gets executed twice on a single thread. How is it possible when I do not do del A ? Worked before the code, but I do not have the commitment to reducing the change that broke it.

The same code fails equally with MY_DICT instead of AMYDTT and as PyDev tells me that it will not work at the time of writing, I am quite confident that something is happening. .

You are probably importing or both of it __ main __ is running as file and importing it.

When Python runs your script, it gives this name from the command line) __ main___ , which is the name of the stored location under sys.modules . But if you import the same file using a import statement, it will be run again and the resulting name will be stored under the name module name.

Similarly, python main.py where main.py contains a import key statement or other code Importing that which is then main in the import will result in all code being played in main.py again.

Another option is to import the module under two different names; Both as part of a package and as standalone modules. This can happen when both HTML

No comments:

Post a Comment