TB1
TB1

Reputation: 299

How to access a variable that gets defined inside a module?

so am writing some modules to use inside a program but am having difficulties using the variable that i define inside the module ex:

a.py

def func():
   #do something to set the variable var
   var = randomValue

b.py

from a import func
func()
#how do i get var

I set var to global but still var is not defined.

all these answers seem great but none of theme work for my script. so here is the module, maybe you can tell me how to get time_converted into another script:

from ib.opt import Connection, message
import time
import datetime

list_time = []
global time_converted
def reply_handler(msg):
    if msg.typeName == "currentTime":
        time = msg.time
        list_time.append(time)
        time_converted = (datetime.datetime.fromtimestamp(int("%s"% time
                                                              )).strftime('%Y-%m-%d %H:%M:%S'))
        return time_converted



def GetTime():
    conn = Connection.create(port=7496, clientId=100)
    conn.registerAll(reply_handler)
    conn.connect()
    conn.reqCurrentTime()
    while True:
        if len(list_time) == 0:
            pass
        elif len(list_time) == 1:
            break
            conn.disconnect()

Upvotes: 1

Views: 70

Answers (3)

TB1
TB1

Reputation: 299

So this is how i was supposed to do it:

GetPrice.py

 from ib.opt import Connection, message
 import datetime

    list_time = []
    def reply_handler(msg):
        if msg.typeName == "currentTime":
            time = msg.time
            list_time.append(time)
            global time_converted
            time_converted = (datetime.datetime.fromtimestamp(int("%s"% time
                                                                  )).strftime('%Y-%m-%d %H:%M:%S'))


    def GetTime():
        conn = Connection.create(port=7496, clientId=100)
        conn.registerAll(reply_handler)
        conn.connect()
        conn.reqCurrentTime()
        while True:
            if len(list_time) == 0:
                pass
            elif len(list_time) == 1:
                break
                conn.disconnect()

        return time_converted

Main.py

from GetRealtime import GetTime

x =  str(GetTime())
print (x)

Thanks for the help guys

Upvotes: 0

Bill Bell
Bill Bell

Reputation: 21643

a.py

from random import random

def func():
    var = random()
    return var

x = func()

b.py

from a import func,x

print (func(), x)

Output from executing b.py:

0.2927063452485641 0.8207727588707955

Edit, in response to modified question

When reply_handler itself receives a callback to process a time it uses another callback to report its result to the main code.

Use a callback function..

1.py

from main import receiver

def GetTime():
    reply_handler('this is the time')

def reply_handler(msg):
    receiver(msg)

GetTime()

main.py

def receiver(message):
    print (message)

Output:

this is the time

Upvotes: 2

Xukrao
Xukrao

Reputation: 8633

You're defining the variable inside a function. To make the value of the variable available outside the function, you need to use a return statement. As an example:

a.py

def func():
    # do something to set the variable `var`
    var = 10

    return var

b.py

from a import func

# to get var
var = func()
print('The value of var is:', var)

See here for more explanation about functions and return statements.

Upvotes: 2

Related Questions