Gentoo Archives: gentoo-user

From: Marc Joliet <marcec@×××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] new machine : Python calculator
Date: Thu, 20 Sep 2012 20:59:39
Message-Id: 20120920225720.16ff815c@marcec.hunte.us
In Reply to: Re: [gentoo-user] new machine : Python calculator by Marc Joliet
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

Attachments

File name MIME type
signature.asc application/pgp-signature