Using exceptions would be the most straightforward approach if the methods other than method1
are private (otherwise external callers would have to handle the Exception):
class MyClass():
class ContinueException(Exception):
pass
def method1(self, members):
for member in members:
try:
print "in method1 with ", member
self.method2(member)
except MyClass.ContinueException:
pass
def method2(self, member):
print "->in method2 with ", member
self.method3(member)
self.method4(member)
def method3(self, member):
print "--->in method3 with ", member
if member % 2 == 0:
raise MyClass.ContinueException()
def method4(self, member):
print "----->in method4 with ", member
When member
is even, an exception will be raised in method3
so method4
won't be called:
>>> c = MyClass()
>>> c.method1([1,2,3,4])
in method1 with 1
->in method2 with 1
--->in method3 with 1
----->in method4 with 1
in method1 with 2
->in method2 with 2
--->in method3 with 2
in method1 with 3
->in method2 with 3
--->in method3 with 3
----->in method4 with 3
in method1 with 4
->in method2 with 4
--->in method3 with 4
>>>