سؤال

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