Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using module variable
#1
Hi All,
i'm trying to learn how to write a module, and meet some problem.
Is there any method we can used a variable inside a module, when we are outside the module.

I have two code, one is the module code(input_mod.py), another one is the main code(main_mod.py).
My question is how can i used the name (which is in the module) this variable in main_mod.py.

i used this print(mod.module.name()), doesn't work.




input_mod.py
def module():
    name = input ("Please enter your name:")
    print ("hell0", name)
    return 
main_mod.py
import input_mod
input_mod.module()
Reply
#2
Just return the name
def module():
    name = input ("Please enter your name:")
    print ("hell0", name)
    return name
	
import input_mod
name = input_mod.module()
Reply
#3
thanks ThomasL

what if i have more than one variable, can we also return more than one variable.
example
def module():
    name = input ("Please enter your name:")
    class1 = input ("Please enter your class:")
    print ("hell0", name)

    return name, class1
import input_mod
aa=input_mod.module()
print (aa)
how can i print name and class like this way==> print ("name:", name, "","class", class1)
is there any method we can do like this
Reply
#4
import input_mod
name, class1 = input_mod.module()
print("Name:", name, "","Class:", class1)
Reply
#5
thanks alot thomasL
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,240 Aug-12-2020, 10:34 PM
Last Post: snippsat
  How I can use multiprocessing with upickled module variable? AlekseyPython 3 4,055 Oct-31-2019, 06:09 AM
Last Post: AlekseyPython
  Unwanted variable change in module dannyH 2 2,667 May-08-2018, 05:33 PM
Last Post: dannyH
  python import module or namespace from namepsace variable ? harun2525 11 8,606 May-23-2017, 05:39 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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