3

I want to call a function from another file and pass arguments from current file to that file. With below example, In file bye.py I want to call function "me" from "hi.py" file and pass "goodbye" string to function "me". How to do that ? Thank you :)

I have file hi.py

def me(string):
    print(string)
me('hello')

bye.py

from hi import me
me('goodbye')

What I got:

hello
goodbye

What I would like:

goodbye
NGuyen
  • 265
  • 5
  • 13

1 Answers1

2

Generally when you create files to be imported you must use if __name__ == '__main__' which evaluates to false in case you are importing the file from another file. So your hi.py may look as:

def me(string):
    print(string)

if __name__ == '__main__':
    # Do some local work which should not be reflected while importing this file to another module.
    me('hello')
ZdaR
  • 22,343
  • 7
  • 66
  • 87