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
2

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.