Python3: to add, remove and change

B

bearophileHUGS

Here an informal list in random order of things that I may like to add
or to remove to/from Python3.x+.

The things I list here don't come from fifty hours of thinking of
mine, and they may be often wrong. But I use Python2.x often enough,
so such things aren't totally random either.

To remove: map and filter (ex-ifilter/imap).
I don't use them often enough, because generator expression are an
obvious way to map and filter, and to me they seem usually good
enough. (So far ShedSkin has done well without map/filter). (they can
be moved into the itertools module, of course).


To add: I use groupby often enough, so I may like to see it as a
builtin.


To change and improve: the API of the "re" module. I think several of
its APIs can be improved and made more intuitive, reducing both memory
and cognitive load. Despite having used the re module many times I
need still to look up details into the docs. So its API may be bad.


To add: Python3 is much more lazy-flavour than Python 2.x (see lazy
range, lazy dict views, etc). So islice becomes even more useful and
more commonly used. So I may like to have a lazy slicing as built-in.
But if you make it built-in, then why not add a syntax too? So I'd
like the usual slicing syntax [::] to be added to all lazy generators
too (to generator expressions, as an automatically created method of
iterators, and of course usable by iterable classes too).
Examples:
foo = (i for i in range(2, 1000) if all(i % d for d in range(2, i)))
list(foo[3: 14]) [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
def bar():
.... for i in range(2, 1000):
.... if all(i % d for d in range(2, i)):
.... yield i
....
[7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]



To change: + is a commutative operation, while the concatenation of
two sequences is not. So after using the D language for some time I
have started to not appreciate anymore to use + to concatenate strings
and lists. D language uses ~ for such operation (this isn't a perfect
symbol, because I don't have it on my keyword, so on Windows I have to
type ALT+126 to insert it, or I have to redefine/remap a keyboard
key).
D also has a specific operator that can be overloaded: opCat, opCat_r
and opCatAssign, that map to ~ and ~= (the _r is the inverted order,
like the __radd__):
http://www.digitalmars.com/d/2.0/operatoroverloading.html#Binary
So if you define a vector-like collection you can define both opAdd
and opCat, the first may the sum between two vectors and the opCat is
their joining.


To add: in Python3 you use lazy constructs much more often, so
itertools.chain may become more used. So it may be good to have chain
as built-in. But then it can be good to have a better syntax support
too. So I'd like all lazy iterables to support chaining. If you use
the ~ operator you can write:
range(100) ~ (i for i in range(2, 1000) if all(i % d for d in range(2,
i)))
(I have implemented most of itertools in D language, and I have added
this ~ operator to all lazy constructs, plus I have added an easy way
to add it to user-defined lazy iterables).


Unladen Swallow for official CPython 2.x/3.x: yes, thank you, if it
will be good enough. It can be what Psyco isn't able to become, and
what PyPy has so far failed to be (PyPy seems Open Souce public-money-
funded vaporwere that has tried to do too much).


To add again: tuple unpacking in function arguments: it's handy, hi-
level, de-clutters the code and shortens it too.


To change: I'd like {:} as empty dict literal, and {} as empty set
literal.


To do nothing: probably I will never fully like the syntax of tuple
literals. It's not clean. But in practice I can live with it, and I
have so far failed to invent a better syntax. The main problem is that
ASCII (and keyboards too, maybe) was created for business purposes and
it has too few delimiters. While the sytax for lists: [] [1] [1, 2, 3]
is perfect.

To add: I use defaultdicts often enough, so I may even like to see it
as built-in. But this isn't essential, using the import from the
collections module is usually acceptable.

Bye,
bearophile
 
M

MRAB

Here an informal list in random order of things that I may like to add
or to remove to/from Python3.x+.
[snip]
To add: Python3 is much more lazy-flavour than Python 2.x (see lazy
range, lazy dict views, etc). So islice becomes even more useful and
more commonly used. So I may like to have a lazy slicing as built-in.
But if you make it built-in, then why not add a syntax too? So I'd
like the usual slicing syntax [::] to be added to all lazy generators
too (to generator expressions, as an automatically created method of
iterators, and of course usable by iterable classes too).
Examples:
foo = (i for i in range(2, 1000) if all(i % d for d in range(2, i)))
list(foo[3: 14]) [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
def bar():
... for i in range(2, 1000):
... if all(i % d for d in range(2, i)):
... yield i
...
[7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
How about adding [x : y : z] as a shortcut for range(x, y, z)?
>>> foo = (i for i in [2 : 1000] if all(i % d for d in [2 : i]))
To change: + is a commutative operation, while the concatenation of
two sequences is not. So after using the D language for some time I
have started to not appreciate anymore to use + to concatenate strings
and lists. D language uses ~ for such operation (this isn't a perfect
symbol, because I don't have it on my keyword, so on Windows I have to
type ALT+126 to insert it, or I have to redefine/remap a keyboard
key).
D also has a specific operator that can be overloaded: opCat, opCat_r
and opCatAssign, that map to ~ and ~= (the _r is the inverted order,
like the __radd__):
http://www.digitalmars.com/d/2.0/operatoroverloading.html#Binary
So if you define a vector-like collection you can define both opAdd
and opCat, the first may the sum between two vectors and the opCat is
their joining.
-1.

To add: in Python3 you use lazy constructs much more often, so
itertools.chain may become more used. So it may be good to have chain
as built-in. But then it can be good to have a better syntax support
too. So I'd like all lazy iterables to support chaining. If you use
the ~ operator you can write:
range(100) ~ (i for i in range(2, 1000) if all(i % d for d in range(2,
i)))
(I have implemented most of itertools in D language, and I have added
this ~ operator to all lazy constructs, plus I have added an easy way
to add it to user-defined lazy iterables).
For lazy concatenation/chaining, perhaps +1.
To add again: tuple unpacking in function arguments: it's handy, hi-
level, de-clutters the code and shortens it too.
+1.

To change: I'd like {:} as empty dict literal, and {} as empty set
literal.
+1.

To do nothing: probably I will never fully like the syntax of tuple
literals. It's not clean. But in practice I can live with it, and I
have so far failed to invent a better syntax. The main problem is that
ASCII (and keyboards too, maybe) was created for business purposes and
it has too few delimiters. While the sytax for lists: [] [1] [1, 2, 3]
is perfect.
[snip]
Could you wrap tuples in <...>? I'd need to check the syntax to see
whether it's unambiguous.
 
R

Raymond Hettinger

To add again: tuple unpacking in function arguments: it's handy, hi-
level, de-clutters the code and shortens it too.

+1 But you will have to talk to Brett about it. He's the one
who led the effort to kill it.

To change: I'd like {:} as empty dict literal, and {} as empty set
literal.

Unless you can time machine back to 1990, I would say that the {}
notation for emtpy dicts is too firmly entrenched. I don't think
it is possible to switch without causing a great deal of unnecessary
pain. Besides, it doesn't hurt much to write: s=set() or d=dict()
which are explicit, clear, and short. That's what I usually do in
my code.

To do nothing: probably I will never fully like the syntax of tuple
literals. It's not clean. But in practice I can live with it, and I
have so far failed to invent a better syntax. The main problem is that
ASCII (and keyboards too, maybe) was created for business purposes and
it has too few delimiters.

« unicode has lots of delimiters »


Raymond
 
J

Joe P. Cool

To change: + is a commutative operation,

No, it _is_ not. It is rather _used_ frequently as a symbol for adding
numbers, which happens to be a commutative operation. For me '+' means
composing instances of a kind. If we are to narrow minded about
generalization we'll end up with a clumsy syntax like Java's.
Unladen Swallow for official CPython 2.x/3.x: yes, thank you, if it
will be good enough. It can be what Psyco isn't able to become, and
what PyPy has so far failed to be (PyPy seems Open Souce public-money-
funded vaporwere that has tried to do too much).

How about moving these lines to a separate message with a matching
header?

Regards, Joe. P. Cool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Python3 string slicing 2
Opening and appending to file in Python3 6
Opening and appending to file in Python3 0
Why Python3 12
python3; 0
sys.stdout and Python3 0
[python3] 6
Python3 html.parser 0

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top