Delegates: Difference between revisions

670 bytes removed ,  16 years ago
Fix python section
(Simplify the task to the minimum required to show the pattern)
(Fix python section)
Line 75:
return 0;
}
 
==[[Python]]==
[[Category:Python]]
 
<pre>
class Delegator:
def __init__(self, delegate=None):
self.delegate = delegate
def thing(self):
if hasattr(self.delegate, 'thing'):
return self.delegate.thing()
return 42
def other(self):
if hasattr(self.delegate, 'other'):
return self.delegate.thing()
return 5
 
class Delegate:
def thing(self):
return 37
 
if __name__ == '__main__':
 
# No delegate
a = Delegator()
assert a.thing() == 42
assert a.other() == 5
 
# With delegate
a.delegate = Delegate()
assert a.thing() == 37
assert a.other() == 5
</pre>
Anonymous user