开发者

NameError: global name 'thing' is not defined

I don't think there is quite the need for the continued downvoting, I am just trying to learn here!

One.py

from two import *

ADooDah = Doodah()
something = Thing(ADooDah)

something.DoThis()
something.DoThat
something.DoAnother
if (something.has_done_stuff() == True)
    self.SomeFunction

Two.py

class Thing(var):
    def __init__(self, var)
        self.SomeVar = var

    def has_done_stuff(self):
        while True:
            id, newMessage = SomeVar.get_next_message()
            if id == 0:
                return true
            else:
                return false

I get...

Traceback (most recent call last):
  File "C:\One.py", line 9, in <module>
    has_done_stuff = thing.HasDoneStuff()
NameError: global name 'thing' is not defined

EDITS: The code was indeed peppered with errors. I was trying to show my situation rather than any real code. Rush typing causes foolish typing. Even I'm not that bad! Well, most of the time ;) .

I hope the 开发者_JS百科edits make it all make more sense and you fine people can stop focusing on the crazy syntax errors and explain a bit more about my scope (I assume) problem. I'm fairly new to Python/IronPython and the rules around implicit types and scoping I am still in the process of learning!

I have solved my problem though. Thanks. It was fairly unrelated to the above as it turns out.


Something = Thing(ADooDah)

thing.DoThis()

Your thing is called Something. Also, your class Thing has none of the methods you are calling/not calling (missing parens). This is pretty much non-sense code.


There are a few issues:

You claim Thing is defined in Two.py. If so, you need to import it thus:

from Two import Thing

or (not recommended):

from Two import *

Next, you need class, not Class.

Next, you need to define thing, which you haven't done. I will take a wild guess that you want thing to be a Thing object:

thing = Thing(ADooDah)

then there is the pfoblem with the if inside HasDoneStuff that someone has referred to in a comment, and the fact that Thing is incomplete (also mentioned in another answer).


I give the following codes.

I don't know for what they will be usable...... But they CAN run.

.

two.py

from time import time

class Thing():
    def __init__(self, var):
        self.SomeVar = enumerate(var)

    def HasDoneStuff(self):
        while True:
            id, newMessage = self.SomeVar.next()
            print newMessage
            print 'id==',id
            return id == 0

    def DoThis(self):
        print "DoThis' result"

    def DoThat(self):
        print 'DoingThat ;;;;;;;;;;;;;;;;;;;;;'

    def DoAnother(self):
        print 'DoAnother time',time()

    def SomeFunction(self):
        print 'Humpty Dumpty sat on a wall'

.

one.py

from two import *


def Doodah(ss):
    return ss.split()

ADooDah = Doodah('once upon a time')

Something = Thing(ADooDah)


Something.DoThis()
Something.DoThat()
Something.DoAnother()

print '\n==========================\n'

while True:
    try:
        if Something.HasDoneStuff():
            Something.SomeFunction()
        print '---------------'
    except StopIteration:
        print "That's all folks"
        break
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜