-2

So I have a module.py like this:

import dataclass

@dataclass
class A:
  x: int
  y: int

  def big_method(self):
      do_big_stuff(x,y)

LIST_OF_A = [ A(2,3), B(4,5) ]

Why when in my script.py when I do

from module import A

the script.py actually ran and instantiated LIST_OF_A ? The only way I fix it is to separate the file from the class definition and the LIST_OF_A. Is there a way to combine it both in one module.py?

mrkgoh
  • 31
  • 2
  • 7

1 Answers1

3

Whenever you import your modules, python runs that module.

In your module file you should write you code inside

def main():
    """Code that is executed only if you run this file itself,
    and ignored if you import it goes here."""
    pass

if __name__ == "__main__":
    main()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69