1 |
[...] |
2 |
> |
3 |
> def myprint(num, places=4, *args, **kargs): |
4 |
> fmt_str = "{:." + str(places) + "f}" |
5 |
> print(fmt_str.format(num), *args, **kargs) |
6 |
|
7 |
OK, quick update because I just realised how weird it is to have positional |
8 |
arguments after a (potential) keyword argument (I really should go to bed). |
9 |
Either of these is better: |
10 |
|
11 |
# "places" is exclusively a keyword argument now |
12 |
def myprint(num, *args, places=4, **kargs): |
13 |
fmt_str = "{:." + str(places) + "f}" |
14 |
print(fmt_str.format(num), *args, **kargs) |
15 |
|
16 |
# doesn't support extra arguments to print(), but is simpler |
17 |
def myprint(num, places=4): |
18 |
fmt_str = "{:." + str(places) + "f}" |
19 |
print(fmt_str.format(num)) |
20 |
|
21 |
-- |
22 |
Marc Joliet |
23 |
-- |
24 |
"People who think they know everything really annoy those of us who know we |
25 |
don't" - Bjarne Stroustrup |