Consistency is an important quality property of a language. One of my main points of critic agains PHP was inconsistency (see PHP: A strange language). Let's see where Python is inconsistant.
Naming
Underscores or not
PEP 8 recommends underscores for functions, if I remember it correctly. However, some built-in functions do not follow this naming scheme:
float.fromhex
bytes.startswith
str.startswith
str.is_digit
os.path.isfile
and probably all otheros
functions.
snake_case or camelCase
Where | Is | Should be |
---|---|---|
Logging module (everywhere) | logging.getLogger | logging.get_logger |
Methods / Properties / Functions
len
: Each container type should have a propertylength
. One the one hand, this is done in many other languages. On the other hand, it indicates that getting the length is a constant-time operation.
Datetime
Have a look at the following code:
import datetime
x = datetime.datetime(2017, 01, 01, 12, 00)
y = datetime.datetime(2017, 01, 02, 12, 00)
print("Seconds elapsed: {}".format((y - x).seconds))
What would you expect the output to be? I expected 24 * 60 * 60
. But it is
0
. What you were looking for is (y - x).total_seconds
.
Tutorial
A tutorial is a method of transferring knowledge and may be used as a part of a learning process. More interactive and specific than a book or a lecture; a tutorial seeks to teach by example and supply the information to complete a certain task.
Source: Wikipedia
Tutorials are very important for programming languages. Especially the ones which introduce the core concepts of a language. I think Python should have 3 tutorials:
- How to install: Detailed instructions for all major systems. At the beginning, the user should be asked about his system (and get help how to find out which system he has) and then get only the instructions necessary for his system.
- Programming beginners Python tutorial: Python is probably the first programming language some people use. And I think it is suited well for them. However, there should be an official Python tutorial for people who have to learn what a variable is and what functions are. This tutorial might contain information which over-simplifies stuff. wiki.python.org/moin/BeginnersGuide is similar to that, but it seems not to be very well structured.
- Advanced programmers Python tutorial: For people who know the basic concepts of programming.
These three tutorials should be linked from python.org/tutorials.
Documentation
Every function / object / method should have examples. So the
len
function should additionally have this:
>>> some_list = [23, 12, -3, 0]
>>> len(some_list)
4
>>> other_list = [[12, 32, 456], [1, 2, 3]]
>>> len(other_list)
2
I think the PHP documentation is awesome. Take a look at
strlen
. It is immediately
clear how to use it, which versions are supported, what types the parameters
should have and what the return value is. Functions which you might have
looked for can be found under "See Also" and some difficulties can be found
under "User Contributed Notes". I have never seen a similar well-structured
and written documentation of a programming language (not for Java, C, C++,
Haskell, JavaScript)
Also, runtime information in big-O notation as well as space information would be nice.
PyPI
gi
is for GTK, but the module https://pypi.python.org/pypi/gi "overrides" it.
That should not be possible. So there is an issue with namespaces.
Joining lists
Joining a list of strings works like this in Python 3:
>>> a = [str(el) for el in range(5)]
>>> a.join(" ")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
>>> " ".join(a)
'0 1 2 3 4'
However, I think it is much more logical to apply a join
method of a list
with a string argument than a join
method of a string with a list argument.
This might be the case because I know
how join is done in JavaScript.
Module specific
Base64
The module base64
contains the functions b16decode
, b16encode
,
b32decode
, b32encode
, b64decode
, b64encode
, decode
, decodestring
,
encode
, encodestring
, standard_b64decode
, standard_b64encode
,
urlsafe_b64decode
, urlsafe_b64encode
.
It is not good to have a module called base64
and give it base32decode
.
But that is probaly for historic reasons and I cannot think of a better name
by now. text_encoding
? That would have "encoding" in the name.
However, I think the module should only have the functions
- str encode(str s, int base=64)
- str decode(str s, int base=64, casefold)
I don't see a good reason why reading and writing should be done by this module.
Scipy - PIL
PIL uses (width, height)
(e.g. in Image.new
) and SciPy uses (height, width)
.