문제

Here is my code:

from SimPy.Simulation import *

class Message(Process):
    def arrive(self, destination):
        yield hold, self, 2
        try:
            print "%s %s going to %s" % (now(), self.name, destination.name)
            self.interrupt(destination)
        except NameError, x:
            print "%s is out of reach" % x

What I want to do is to print out that destination is unreachable when its name doesn't exist, but I'm still getting usual python error:

Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    message.arrive(node2)
NameError: name 'node2' is not defined
도움이 되었습니까?

해결책

Your name error does not occur in the method. It occurs before the method is called.

Python tries to resolve node2 before it can pass the value of node2 to the message.arrive() method. The method code is never executed.

You'd get the same error if you just typed node2 in your shell, you did not define it so Python doesn't know how to use it's value then either.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top