#Why does importing list from another file also imports functions?

10 messages · Page 1 of 1 (latest)

crude zealot
#

Hello, I'm doing an exercise from the python crash course and thought about using the import statement I read about earlier.
``
from guest_list import invitees
print(invitees)

output:
I would like to invite you to dinner Npc1.
I would like to invite you to dinner Npc2.
I would like to invite you to dinner Npc2.
['npc1', 'npc2', 'npc3']


This is the content of guest_list:

###Exercise 3-4. Guest List

invitees = ["npc1", "npc2", "npc3"]

msg1 = f"I would like to invite you to dinner {invitees[0].title()}."

print(msg1)

msg2 = f"I would like to invite you to dinner {invitees[1].title()}."

print(msg2)

msg3 = f"I would like to invite you to dinner {invitees[1].title()}."

print(msg3)

``

burnt marten
#

What functions?

crude zealot
#

Oh, sorry I meant the value of a function if that makes sense?

burnt marten
#

Oh, why is it printing when you import?

crude zealot
#

yes why is it printing I would like to invite you to dinner Npc1. etc

#

I want it to only print the list

burnt marten
#

Because in order to import anything you have to run the code to create the stuff you want. So if there are prints they will run.

#
from guest_list import invitees

Is essentially the same as

import guest_list
invitees = guest_list.invitees
del guest_list
#

So the module is being imported first and then it gets the thing you want and puts it into a variable.

crude zealot
#

oh, I see so it's best to just copy the list manually in my case