Python Forum
Duplicate output when calling a custom function from the same file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Duplicate output when calling a custom function from the same file?
#1
Hi folks,

I'm new to programming and python, and I don't understand why if you call a custom function from the same file it's defined in, you get duplicate output.

e.g. a file "question.py" containing the following:
def double(number):
    return number * 2

from question import double
print(double(1))
And if I were to manually input "double(2)" after running the module, the output seems perfectly normal (just one 4).
Why is that? And is there a way to avoid the duplicate apart from calling the function from a separate py file?

I know this must have been asked before, but I couldn't find it via forum search or Google (the queries all return questions related to list modification).

Thanks in advance for clearing up my confusion!
Reply
#2
First of all - you don't need to import it if it is in the same module.
it is executed twice, because it is executed once during the import (line 4) and once when you call it on line 5. Remember that even if you use from ... import ... it's always the whole module being imported, but only the particular name imported is visible.

In more general context - if you have code that you don't want executed during import - that's where the if __name__ == '__main__': comes in handy:

let's say you have module named module1.py :

def double(number):
    return number * 2
 
if __name__ == '__main__':
    print(double(1))
and in another module named module2.py:

from module1 import double
print(double(2))
if you run module1 from command line then the body of the if __name__ == '__main__': would be executed and will print 2.
if you run module2 from command line, it will be imported (the whole module, but only name double will be visible), that part (line 5) will not be executed during the import and the only code executed would print 4.
(May-10-2019, 07:38 AM)road2knowledge Wrote: is there a way to avoid the duplicate apart from calling the function from a separate py file
Note that if you have in module1
def double(number):
    return number * 2

print(double(1))
and in separate module module2.py have

from module1 import double
print(double(2))
you will still get double output (i.e. being in separate file will not stop print(double(1)) being executed during the import in module2
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you so much explaining all that!
I think I finally understand it now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 Nov-23-2023, 02:53 PM
Last Post: rob101
  problem in output of a function akbarza 9 1,094 Sep-29-2023, 11:13 AM
Last Post: snippsat
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 766 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  How to print the output of a defined function bshoushtarian 4 1,234 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,754 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  output correction using print() function afefDXCTN 3 10,960 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  python prints none in function output chairmanme0wme0w 3 2,157 Jul-07-2021, 05:18 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020