Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce06e8b

Browse files
committed
Highlight the need for a wrapper in delegate
- It's required to capture the args/kwargs when called at run time. - Fix doctest output to include hints
1 parent 9016858 commit ce06e8b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

‎patterns/fundamental/delegation_pattern.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ class Delegator:
1919
>>> delegator.p2
2020
Traceback (most recent call last):
2121
...
22-
AttributeError: 'Delegate' object has no attribute 'p2'
22+
AttributeError: 'Delegate' object has no attribute 'p2'. Did you mean: 'p1'?
2323
>>> delegator.do_something("nothing")
2424
'Doing nothing'
25+
>>> delegator.do_something("something", kw=", faif!")
26+
'Doing something, faif!'
2527
>>> delegator.do_anything()
2628
Traceback (most recent call last):
2729
...
28-
AttributeError: 'Delegate' object has no attribute 'do_anything'
30+
AttributeError: 'Delegate' object has no attribute 'do_anything'. Did you mean: 'do_something'?
2931
"""
3032

3133
def __init__(self, delegate: Delegate) -> None:
@@ -47,8 +49,8 @@ class Delegate:
4749
def __init__(self) -> None:
4850
self.p1 = 123
4951

50-
def do_something(self, something: str) -> str:
51-
return f"Doing {something}"
52+
def do_something(self, something: str, kw=None) -> str:
53+
return f"Doing {something}{kwor''}"
5254

5355

5456
if __name__ == "__main__":

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /