0

I created the mySimpleFile.py file with class "MySimple"

class MySimple (object):
    def __init__(self, name):
        self.name = name
        print("from Class",self.name)
 
print ("I do not want this automatically!!!")

Now I want to import this class to my another file.

import mySimpleFile
 
print("bla bla bla")

So I get this:

I do not want this automatically!!! 
bla bla bla

So how can I disable this initialization?

Thank you!

3 Answers3

0

Put it in an if __name__ block so that it's only executed if it's the "main" script file, and not if it's imported:

if __name__ == '__main__':
    print ("I do not want this automatically!!!")
Samwise
  • 51,883
  • 3
  • 26
  • 36
0

Use the following idiom:

if __name__ == '__main__':
    print ("I do not want this automatically!!!")

This feature is specifically there to run a portion of code when the file is run directly, not when it's imported.

ForceBru
  • 41,233
  • 10
  • 61
  • 89
0

add

if __name__ "__main__":
    print("I don't want this")

So when you'll import this file, the print won't be execute because it's not the main file

magimix
  • 74
  • 2