Python-calling method outside of class
Frage
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?
Lösung
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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow