Skip to content

Commit 7cc978c

Browse files
committed
Updated.
1 parent bd9412d commit 7cc978c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

Decorators__examples/combine_decorators__with_args.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def actual_decorator(func):
2020
@functools.wraps(func)
2121
def wrapped(*args, **kwargs):
2222
attrs = get_attrs_str(decorator_kwargs)
23-
return f"<b{attrs}>" + func(*args, **kwargs) + "</b>"
23+
return f"<b{attrs}>{func(*args, **kwargs)}</b>"
2424

2525
return wrapped
2626

@@ -32,7 +32,7 @@ def actual_decorator(func):
3232
@functools.wraps(func)
3333
def wrapped(*args, **kwargs):
3434
attrs = get_attrs_str(decorator_kwargs)
35-
return f"<i{attrs}>" + func(*args, **kwargs) + "</i>"
35+
return f"<i{attrs}>{func(*args, **kwargs)}</i>"
3636

3737
return wrapped
3838

@@ -58,12 +58,37 @@ def deco(f):
5858

5959
def multi(func):
6060
return composed(
61+
custom_tag(
62+
name="a",
63+
href="https://example.com",
64+
title="Hint!",
65+
),
6166
makebold(foo="1"),
6267
makeitalic(bar="2"),
6368
upper,
6469
)(func)
6570

6671

72+
def custom_tag(
73+
name: str,
74+
**arguments,
75+
):
76+
def actual_decorator(func):
77+
@functools.wraps(func)
78+
def wrapped(*args, **kwargs):
79+
attrs = get_attrs_str(arguments)
80+
return f"<{name}{attrs}>{func(*args, **kwargs)}</{name}>"
81+
82+
return wrapped
83+
84+
return actual_decorator
85+
86+
87+
@custom_tag(
88+
name="a",
89+
href="https://example.com",
90+
title="Hint!",
91+
)
6792
@makebold(foo="1")
6893
@makeitalic(bar="2")
6994
@upper
@@ -77,7 +102,7 @@ def hello_2(text):
77102

78103

79104
print(hello("Hello World!"))
80-
# <b foo="1"><i bar="2">HELLO WORLD!</i></b>
105+
# <a href="https://example.com" title="Hint!"><b foo="1"><i bar="2">HELLO WORLD!</i></b></a>
81106

82107
print(hello_2("Hello World!"))
83-
# <b foo="1"><i bar="2">HELLO WORLD!</i></b>
108+
# <a href="https://example.com" title="Hint!"><b foo="1"><i bar="2">HELLO WORLD!</i></b></a>

0 commit comments

Comments
 (0)