Question

I imported a simple module into my Python shell. It contains just the following function (what it does it not important for my problem)

 def generateMessages():
        messagesPerSecond = [getPoisson(rate1) for i in range(0, t)]
        messages = []
        while i < t:
            while j < messagesPerSecond[i]:
                millisecond = round(random.random(), 3)
                messages.append(i + millisecond)
                j += 1
            i += 1

When I want to test this out in shell, I set the variables

t = 100
rate1 = 3

and call generateMessages().

But then I get an error NameError: global name 't' is not defined. But it is defined!

Why is this happening? How do I stop this?

Était-ce utile?

La solution

Globals in the shell are not the same globals as what generateMessages() sees. Each module has their own.

Set t in the module generateMessages() is defined in.

You can do this dynamically:

>>> import yourmodule
>>> yourmodule.t = 100
>>> yourmodule.rate1 = 3
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top