I come from ruby/C# and am new to Python.

I'm looking at the following code:

def raiseFlag():
    global flag
    flag = 1

class TermStructureTest():

    def testImpliedObs(self):
        global flag
        flag = None
        h = RelinkableYieldTermStructureHandle()
        settlement = self.termStructure.referenceDate()
        new_settlement = self.calendar.advance(settlement,3,Years)
        implied = ImpliedTermStructure(h,new_settlement)
        obs = Observer(raiseFlag)
        obs.registerWith(implied)
        h.linkTo(self.termStructure)
        if not flag:
            self.fail("Observer was not notified of term structure change")

Why does is it work to call "raiseFlag" within the TermStructureTest() class?

有帮助吗?

解决方案

Functions defined in the top level are said to be defined at the module (file) level. So if you're in the same file it's globally accessible.

If you're in a different file you'd need

import foo #then use foo.raiseFlag()

or

from foo import raiseFlag #use raiseFlag()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top