Add _KB, _MB, _GB to numeric literals
PEP 515 added underscore to numeric literals, it brings better readability. PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/ Is it possible to add _KB, _MB, _GB to numeric literals, for example: 200_KB (200*1024) 150_MB (150*1024*1024) 2_GB (2*1024*1024*1024) Do you think it's a good code style?
On 10/13/20 1:06 AM, malincns@163.com wrote:
PEP 515 added underscore to numeric literals, it brings better readability.
PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/
Is it possible to add _KB, _MB, _GB to numeric literals, for example:
200_KB (200*1024) 150_MB (150*1024*1024) 2_GB (2*1024*1024*1024)
Do you think it's a good code style?
That is easy enough to do with just a slight change in syntax: KB = 1024 MB = 1024*1024 GB = 1024*1024*1024 200*KB 150*MB 2*GB -- Richard Damon
13.10.20 08:06, malincns@163.com пише:
PEP 515 added underscore to numeric literals, it brings better readability.
PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/
Is it possible to add _KB, _MB, _GB to numeric literals, for example:
200_KB (200*1024) 150_MB (150*1024*1024) 2_GB (2*1024*1024*1024)
Do you think it's a good code style?
First, according to modern standard, 1 kilobyte contains 1000 bytes. 1024 bytes is 1 kibibyte. But in some applications 1 kilobyte means 1024 bytes. I do not even want to mention applications in which 1 "megabyte" is equal to 1000*1024 bytes. It all leads to confusion. Second, why use bytes units? Not every integer value measures the amount of memory. If you multiply 2 bytes by 3 bytes, do you get 6 square bytes? If you divide 60 by 5 bytes, do you get 12 reversed bytes?
Second, why use bytes units? Not every integer value measures the amount of memory. If you multiply 2 bytes by 3 bytes, do you get 6 square bytes?
If a code executes m_bytes * n_bytes, it's probably a logic error. If all values have a unit in a programming language, it might help us to check such errors.
On Wed, Oct 14, 2020 at 6:56 PM Ma Lin <malincns@163.com> wrote:
Second, why use bytes units? Not every integer value measures the amount of memory. If you multiply 2 bytes by 3 bytes, do you get 6 square bytes?
If a code executes m_bytes * n_bytes, it's probably a logic error.
If all values have a unit in a programming language, it might help us to check such errors.
There ARE unit systems for Python, but the native int type deliberately doesn't have an associated unit. ChrisA
malincns@163.com writes:
PEP 515 added underscore to numeric literals, it brings better readability.
PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/
Is it possible to add _KB, _MB, _GB to numeric literals, for example:
With a bit of generalization, it's something that many scientific users would like to see. But I think if you follow the idea to some logical extensions, you'll see that it's not a good idea. The problem is that adding them to literals would require that they be built in to the parser. But why stop at _KB? Surely you need _KiB and _km and _kg? Should _m_s be interpreted as meter-seconds or meters/second? Look up the units and siunits modules on PyPI for two examples of how units could be handled in Python. A few years ago there was a long discussion about the design of a somewhat more powerful module like siunits that could added to the standard library, and perhaps be given a syntactic role eventually (so that you could write 10 kb instead of 10*kb). I don't have time or energy to look up the thread, but siunits seems to have most of the features proposed, so I doubt it's worth your time to chase it down youself. Unless you want the *really* *long* version of the paragraph above. :-) Steve
I would also suggest taking a look at human friendly https://pypi.org/project/humanfriendly/ which does a lot this for you on the user input/output front. Steve Barnes From: malincns@163.com<mailto:malincns@163.com> Sent: 13 October 2020 13:14 To: python-ideas@python.org<mailto:python-ideas@python.org> Subject: [Python-ideas] Add _KB, _MB, _GB to numeric literals PEP 515 added underscore to numeric literals, it brings better readability. PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/ Is it possible to add _KB, _MB, _GB to numeric literals, for example: 200_KB (200*1024) 150_MB (150*1024*1024) 2_GB (2*1024*1024*1024) Do you think it's a good code style? _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/QIKCXS... Code of Conduct: http://python.org/psf/codeofconduct/
On 2020-10-13 06:06, malincns@163.com wrote:
PEP 515 added underscore to numeric literals, it brings better readability.
PEP 515 -- Underscores in Numeric Literals https://www.python.org/dev/peps/pep-0515/
Is it possible to add _KB, _MB, _GB to numeric literals, for example:
200_KB (200*1024) 150_MB (150*1024*1024) 2_GB (2*1024*1024*1024)
Do you think it's a good code style?
A couple of points: 1. 200_KB is really a size in bytes, not a number, if you see what I mean. In general, I'm -1. 2. The use of K/M/G as powers of 1024 is an old, though common, convention. These days it's recommended to keep those prefixes as powers of 1000 as in the metric system (SI) (although SI uses 'k' instead of 'K') and use Ki/Mi/Gi instead as powers of 1024.
Sorry for the late response on this. I tried to respond earlier but was stymied by issues with the mailing list server. The last time this was discussed I was the one to raise the suggestion. You can find the start of the thread: 25 Aug 2016: SI scale factors in Python https://mail.python.org/archives/list/python-ideas@python.org/message/HVHRIR... The gist of the suggestion was that SI scale factors have been an internationally standardized way of writing real numbers for over 50 years and is a much nicer than using exponential notation, so wouldn't be a good idea to add support for SI scale factors directly into the language. There was considerable discussion but no consensus emerged for changing the base language. However, the discussion bore fruit in QuantiPhy (https://quantiphy.readthedocs.io), a Python package that incorporates the ideas and suggestions that came up during that discussion. So, while it is still not possible to use SI scale factors in Python real literals, you can use this package to easily use SI scale factors in IO. -Ken
participants (9)
-
Chris Angelico
-
Ma Lin
-
malincns@163.com
-
MRAB
-
python@shalmirane.com
-
Richard Damon
-
Serhiy Storchaka
-
Stephen J. Turnbull
-
Steve Barnes