Python slang
Steven D'Aprano
steve+python at pearwood.info
Fri Aug 5 21:14:20 EDT 2016
On Sat, 6 Aug 2016 08:00 am, Marco Sulla wrote:
> I have a simple curiosity: why Python has much keywords, and some
> builtin types and methods, that are different from the other
> languages? What is the rationale?
You should ask those other languages. Which languages do you have in mind?
> I'm referring to:
> * `except` instead of `catch`
Because this isn't a game of "catch the ball". They're called "exceptions",
not "catchions". You *try* something, and if an *exception* happens, the
except block runs.
In English, without any abbreviations:
try doing this:
code block
except if a TypeError occurs, do this code:
exception handler
> * `raise` instead of `throw`
Because this isn't a game of catch.
> * `self` instead of `this` (I know, it's not enforced, but it's a de
> facto standard and some IDEs like PyDev gives you an error if you
> declare a non-static method without `self` as first parameter)
Because "this" is a stupid name.
In English, we refer to ourselves in the first person as I, me, myself, and
sometimes "self", never as "this". One can say "this one has a hat", for
example, but it sounds weird, like something the Borg would say about a
specific Borg unit.
> * `dict` instead of `map`
Because "map" is a piece of paper showing a visual representation of a
territory, and a dict (dictionary) is a set of words (the keys) followed by
their definitions (the values).
> * `list.append()` instead of `list.push()`
You don't push onto a list, you push onto a stack. You append to a list.
> * `str.strip()` instead of `str.trim()`
Because "trim" is a stupid name for stripping whitespace from the ends of a
string. When you trim hair or a plant, you unconditionally cut it and make
it shorter. If strings had a "trim" method, it should look like this:
def trim(self, distance):
return self[distance:-distance]
and similar for left-trim and right-trim.
> * `True`, `False` and None instead of `true`, `false` and `none` (they
> seems classes)
You'd have to ask other languages why they use 'true', 'false' and 'none'
(they seem like ordinary variables).
> * and furthermore much abbreviation, like `str` instead of `string`
> and `len()` instead of `length()`, that seems to contrast with the
> readability goal of Python.
Just because something is short doesn't make it less readable. According to
Wolfram Alpha, the average length of words in English is 5.1 characters:
http://www.wolframalpha.com/input/?i=average+english+word+length
but of the ten most common words themselves, nine are of three or fewer
characters:
"the of to and a in is it you that"
> I don't ask about `None` instead of `null` because I suppose here it's
> a matter of disambiguation (null, in many languages, is not equal to
> null).
Really? Which languages? That's not true in Pascal, C, Ruby or Javascript.
In Pascal, nil = nil:
[steve at ando pascal]$ cat nil_equal.p
program main(input, output);
var
a,b: ^integer;
begin
a := nil;
b := nil;
writeln(a = b);
end.
[steve at ando pascal]$ gpc nil_equal.p
[steve at ando pascal]$ ./a.out
True
In C, nul == nul:
[steve at ando c]$ cat null_equal.c
#include<stdio.h>
int main()
{
int *a = NULL;
int *b = NULL;
printf("%d\n", a==b);
return 0;
}
[steve at ando c]$ gcc null_equal.c
[steve at ando c]$ ./a.out
1
In Ruby, nil == nil:
irb(main):001:0> a = nil
=> nil
irb(main):002:0> b = nil
=> nil
irb(main):003:0> a == b
=> true
In Javascript, null == null:
js> a = null
null
js> b = null
null
js> a == b
true
I'd like to know what are these "many" languages where null != null.
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list