Some "pythonic" suggestions for Python

  • Thread starter Frank Samuelson
  • Start date
C

Carl Banks

So you like my ideas too!



And python does not use idioms from other languages?

Python doesn't haphazardly include features from other languages, no.
Any feature it borrows from another language will get the same
deliberation as an original suggestion gets, which is quite a lot.
My impression was that "consistency" was important to Python.

Important, yes. All-important, no.
"Consistency" improves my productivity because I don't have to keep
referring to the manual. Things work the way I expect them to work.

Really, should we be taking suggestions from someone who needs a manual
to recall the syntax of the def statement?

What you say is correct in principle, but it's senseless to apply it to
something you use every day, like def. It's like arguing that irregular
verbs make speech less productive.
Of course, they're thought out: They're stolen from another language.

That's not well-thought out.

(Most language features are not well-thought out enough for their own
langauge, let alone in a new environment where well-thought-out-ness is
critical.)
Specifically, the language in which I am most productive.


Only a couple years at it, but that is true of all of the languages that
I know, I guess...


The changes are not arbitrary. They are logical, consistent, less
arbitrary and thus more productive.

The last doesn't necessary follow from the first three.
If such changes are a lost cause,
that is too bad, because it implies that Python will stagnate.

This doesn't even make sense, and is certainly not borne out by history.
Unfortunately that appears the case. Though backward compatibility is
not an issue (3.0 breaks stuff), I have learned that there are many
pythonistas who make up lots of arbitrary reasons not to change
anything, even if it is for the better.

I'm getting the feeling that to you, "arbitrary" is an synonym for
"something not pure enough for my personal taste".
Apparently you missed my statement about loving Python. I love it
because it is the second most productive language I have ever used,
though I do believe it has the potential to be the greatest ever by far.

You probably should go back to #1, then. The "defects in productivity"
in Python aren't going to be "fixed", and nobody's going to listen to you
as long as you justify your suggestions with such a narrowminded argument.



Carl Banks
 
F

Frank Samuelson

Carl said:
Really, should we be taking suggestions from someone who needs a manual
to recall the syntax of the def statement?

What you say is correct in principle, but it's senseless to apply it to
something you use every day, like def. It's like arguing that irregular
verbs make speech less productive.

They do for people who speak foreign languages. It's always easier
for me to remember Spanish verbs that conjugate regularly. And based
on the speaking snafus of my French coworker, I would say that it is
true for her English too!

Likewise, consistency helps me remember the syntax of the
seven or so programming languages that I use regularly.

I'm getting the feeling that to you, "arbitrary" is an synonym for
"something not pure enough for my personal taste".

That was the point that I was trying to make to the previous poster,
who called my suggestions "arbitrary",
but who never actually addressed the suggestions.

The "defects in productivity"
in Python aren't going to be "fixed",

That doesn't sound very promising, though I'm not sure whom you are
quoting.
 
T

Terry Reedy

| My impression was that "consistency" was important to Python.

Important, but not over-riding of all else.

| "Consistency" improves my productivity because I don't have to
| keep referring to the manual. Things work the way I expect them
| to work.

Different people have different expectations. I noticed soon after
learning Python that def, class, and import statements are alternate and
special forms of name-binding statements. Some forms of import can be done
(more awkwardly) as = assignment with the __import__ function. Indeed, if
the name of the file to import is not known until runtime (as a string),
then the import must be so done. But I never expected function definition
to done that way. Perhaps this is because it is special cased in the
previous languages I have used, from Fortran to C.

To me, complete consistency would demand a syntax like

name = func('name', ('param1', ....), '''
<suite statements>
''')

While anonymous data objects are fine, anonymous code is not.

| Though backward compatibility is not an issue (3.0 breaks stuff),

While there will be minor breakage, backward compatibility was most
definitely an issue in the design of Python 3 (not pretty well done as far
as syntax goes).

PS. If you did not want the hostility you perceive, then I think your
title was a mistake. Calling your proposals 'pythonic' invites
disagreement and distracts from discussion of their merits.

Terry Jan Reedy
 
C

Cliff Wells

I love Python, and it is one of my 2 favorite
languages. I would suggest that Python steal some
aspects of the S language.

In general, I agree that Python has some antiquated concepts at its core
(statements being a major one) and that some of these things make the
language inconsistent and introduce additional syntax.

However, despite the apparent close-mindedness of some of the
respondents (it doesn't look like current Python syntax, hence it's
"ugly"), I also agree with them to a degree: changing Python at this
level would mean it would no longer be Python, it would be some other
language. What you are basically asking is that GvR abandon Python
(like ABC before it) and start on a "next generation" language. That's
quite a request. I'm not saying it's a bad request, simply that you
should recognize the scale of request you are making.

On a more practical level, you might take a look at Boo. It's
intentionally Python-like in appearance, yet lifts several key
limitations (and is probably far more open to suggestions at this early
stage in its life). Unfortunately it's nowhere near Python in
completeness (notably documentation, size of community and overall
maturity) and, for better or worse, it runs on .NET/Mono.

Regards,
Cliff
 
B

Bruno Desthuilliers

Frank Samuelson a écrit :
My bad! In my rush I forgot that python requires the return statements.
You people think I'm trying to push lisp, which I am not.

Did I mention Lisp ??? No need to go lispish for this - Javascript
already does what you want here. FWIW, I was somewhat thinking out loud,
and I guess I thought of Javascript's use of anonymous functions, then
switched to Ruby (and it's code blocks), which took me to
expression-based languages. That's all.
(Apparently
many people do, because you are rather skittish.)

I don't get the point here.
In S the value of the last executed statement of a function is the
returned value of the function by default,

Which is what expression-based languages usually do - cf Ruby and most
functional languages.
if there is no return()
function.

Actually, in most languages, it's a statement !-)
It is very convenient for writing really small functions.

For really small functions, Python has lambda - which actually work that
way.
But skip that
for now.

foo = function(x,y) { # S language
if (x>2) return(x+y*2)
x+y
}

foo = func(x,y): # My suggested python syntax (Improved!)
if x>2: return x+y*2
return x+y

# put the function object in the code wherever you want it.
bb= bar( a,b, func(x,y): x=x*2 ; b=4 ; return x*b+y )

bb = bar(a, b, func(x,y):
for x in y:
if x > 10:
do_something_with(x)
return y[-1] * 42

ho, wait, I have a couple keyword args to pass to bar too... How should
I do ? add a comma after the return statement ???

Franck, believe us, *this has been beaten to hell and back*. The point
is not that full anonymous functions are a bad thing (I personally use
them a lot in Javascript), but that it just doesn't fit within Python,
and that the necessary modifications would yield a mostly different
language. Perhaps a pretty good one, but not Python anymore. You see, I
don't necessarily share all Python's design choices, and I'm well aware
of some restrictions they impose. But day in, day out, it happens that
these choices are mostly coherent, and that the resulting language
offers a (IMHO) great balance between simplicity and power.
Non-programmers can (and do) use it, C programmers can (and do) use it,
Java programmers can (and do) use it, Lisp programmers can (and do) use
it, etc, etc, etc.
While I'm at it, classes are objects too. Assign names to them the
same way you assign names to _any_ other object: "="

Same problem. Note that in both cases, you already *can* build functions
and classes that way - as you say, they are objects, and both the def
and class statements are somewhat syntactic sugar. The only point is
that, to instanciate a function, you have more to do:

Help on class function in module __builtin__:

class function(object)
| function(code, globals[, name[, argdefs[, closure]]])
|
| Create a function object from a code object and a dictionary.
| The optional name string overrides the name from the code object.
| The optional argdefs tuple specifies the default argument values.
| The optional closure tuple supplies the bindings for free variables

The Pythonic way to write "more complicated yet still comprehensible
list comprehensions" is to not use list comprehensions.

b = []
for x in vec1:
if x > 0:
for y in vec2:
if y > x:
b.append(x + y)

You have just demonstrated that list comprehensions are not really
necessary at all. So given the "one clear way of doing things",

Actually, it's: "there should be one - and preferably only one - obvious
way to do it". List comps are the obvious way for simple list
operations, and for loops are the obvious way for complex operations. If
you're in for Python Zen quotes, here are some others:
"""
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
"""

shouldn't they be removed? My point is that if you are going to
have them, why make some new unstructured syntax for them?

Because there's already a (well-known) structured syntax for the cases
that are too complex to fit a single, simple expression. It's a question
of balance between purity, readability, and practicallity.
 
B

Bruno Desthuilliers

Frank Samuelson a écrit :
(snip)
The changes are not arbitrary.

Which ones ?
They are logical, consistent, less
arbitrary and thus more productive.

For who ?
If such
changes are a lost cause, that is too bad, because
it implies that Python will stagnate. Unfortunately that appears the case.

This is totally ridiculous. Let's see : list comprehensions, a new,
widely improved object system, with metaclasses, and the descriptor
protocol which brings customisable computed attributes -, lexical
closures, iterators, generator expressions, syntactic sugar for function
decorators, contexts (the 'with' statement), and coroutines. Just to
name a few, and only talking of production releases. Which other
language grew so many new features in the last seven years ? Java ? C ?
C++ ? Lisp ? VB (lol) ?

(snip)
Apparently you missed my statement about loving Python. I love it
because it is the second most productive language I have ever used,
though I do believe it has the potential to be the greatest ever by
far.

I don't think you'll gain that much productivity by fighting against the
language trying to write <whatever-other-language> in it.
 
S

Steven D'Aprano

Of course, they're thought out: They're stolen from another language.
Specifically, the language in which I am most productive.

No, they aren't fully thought out *at all*. To wit:

* you've suggested crippling tracebacks, throwing away the most important
piece of information they currently provide, just because you don't like
the def statement;

* you've suggested allowing sequences as indices into lists, completely
unaware that your two suggestions for what it should mean are mutually
incompatible;

* you've suggested getting rid of the slice syntax for no advantage;

* you've suggested making the function constructor itself a function, and
seem to be completely unaware that your request would need a very
different syntax to that Python already uses;

* you suggest two different versions for the function constructor, one
that is an expression and one that is a suite, but judging by your
comments you don't even realize they are two different versions.


Need I go on?


If you want your suggestions to be taken seriously, you need to think
them through carefully. Well-designed programming languages are not like
chop suey, where you grab whatever left-overs you have in the fridge and
mix them all together because you like the individual ingredients.
 
P

Paul Boddie

They do for people who speak foreign languages. It's always easier
for me to remember Spanish verbs that conjugate regularly. And based
on the speaking snafus of my French coworker, I would say that it is
true for her English too!

But where do you seek consistency? It's true that "class" and "def"
both bind names in a namespace, and I either recall or have imagined
bizarre suggestions where "def" would be used for normal assignments,
but is the "inconsistency" really a problem? At some point you still
need to have a special syntactic component which says that, in the
case of a function, some code is going to follow (yes, "lambda" can do
this to an extent already, and people have argued for multi-line
lambdas to no avail), and similar cues are needed for classes. You
can't wipe away the things which provide meaning in the name of
consistency.
Likewise, consistency helps me remember the syntax of the
seven or so programming languages that I use regularly.

One can actually make an argument that people using noticeably
different natural languages are less likely to mix them up than people
using more similar languages; perhaps one can make the case for
programming languages too. For example, do people mix up Python and
SQL by accident? Java and C++?

[...]
That doesn't sound very promising, though I'm not sure whom you are
quoting.

I started tracking some of the complaints about Python and comparing
them to Python 3000's supposed remedies. It's interesting reading:

http://wiki.python.org/moin/PythonWarts

Paul
 
T

Terry Reedy

| I started tracking some of the complaints about Python and comparing
| them to Python 3000's supposed remedies. It's interesting reading:
|
| http://wiki.python.org/moin/PythonWarts

For ' Inconsistent/unpredictable comparisons/ordering' you write 'no
remedy'. Actually, there will be a major change in 3.0, one that some have
asked for even if it is not what Mertz in particular wants. (I am not sure
he will think after reading his article.)

I think you can regard 'No do statement' as rejected. Some consider the
non-proliferation of loop constructs a feature. I believe this includes
GvR.

There are a number of shortcomings that are being fixed but are not
listed -- at least one for every new feature. So the list you present
strikes me as a somewhat biased.
 
T

Tim Roberts

Diez B. Roggisch said:
Overall, I'd say you don't stand a chance that your proposals will be
adopted. They are minor variations of things that have been proposed &
rejected too often to count - and to be honest: it get's tiresome
beating the same old horses again and again...

That's a bad sign. A language like Python attracts new people all the
time, and those people are going to go through the same evolutionary
learning processes that the long-timers did. They are entitled to
rediscover the things that have already been discovered.

It's fine to say "those ideas have been proposed and rejected, see the
PEPs", but I hope this group retains the tolerance that I have long
admired.
 
N

Neil Cerutti

I think you can regard 'No do statement' as rejected. Some
consider the non-proliferation of loop constructs a feature. I
believe this includes GvR.

Non-providing of the standard loop constructs, yes. But loop
constructs are plenty proliferate.
 
L

Loic Mahe

Chris M write :
Multi-return value lambda? Just so you know, there is no concept of
returning more than one value from a function.
> * multi return value lambda

I meant: multiple return statement, not return multiple values

pseudo code here:

lambda x: if A return B, if C return D, if E return F ...

it could be nice if ou could write normal functions ... as lambda ones
and not being limited to a subset of the kewords ...

at least it would fit Frank Samuleson needs/wishes better...
 
H

Hrvoje Niksic

Loic Mahe said:
Chris M write :


I meant: multiple return statement, not return multiple values

pseudo code here:

lambda x: if A return B, if C return D, if E return F ...

You can do that with conditional expressions:

lambda x: B if A else D if C else F in E ...
it could be nice if ou could write normal functions ... as lambda
ones and not being limited to a subset of the kewords ...

It becomes natural if you think of lambda as a deferred expression
rather than as an anonymous function.
 

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

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
IRMNikole

Latest Threads

Top