[Python-ideas] Python Reviewed

Chris Kaynor ckaynor at zindagigames.com
Mon Jan 9 15:26:45 EST 2017


On Mon, Jan 9, 2017 at 11:40 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, Jan 09, 2017 at 07:25:45PM +0800, Simon Lovell wrote:
>>     Lack of a with statement which only obscures the code
>Python has a `with` statement.

I suspect Simon means similar to the VB with statement, which allows
an object to become the default namespace.

Basically, rather than:
object.alpha()
object.beta()

you can do:
with object:
    alpha()
    beta()

or some slight variant thereof. Both cases do the same thing.

Personally, I vastly prefer the explicit naming of self, and it should
be noted that the style guides I have seen for C/C++ code have
required member variable names to be prefixed with something like "m_"
or just "_" to keep them from getting confused with local variables.
Python solves this problem by requiring the object reference, and
generally, I have found that much of the time it does not add that
much extra to the code to have the explicit references, while making
it very clear what is intended.

>>     No do-while construct
>
> What do you mean by "do-while" and how do you expect it to differ from
> "while"?

This one is somewhat common in other languages, and a do-while
executes at least once (checks the condition at the end), while while
executes at least zero times (checks the condition at the start).

In C:
do
{
    ..block..
} while (condition);

is the same as:

..block..
while (condition)
{
    ..block..
}

where both "..block.." are the same. The exact same result can be
gotten with (in Python):

while True:
    ..block..
    if condition:
        break

That said, I've only occasionally had use for the construct, and the
most common case I've seen for it is multi-line macros in C/C++ where
it is needed to get proper handling to require a semicolon at the end
of the macro invocation and handle the optional braces in the flow
control structures.

Almost always, if checking the condition at the start is not good
enough, I almost always want to check the condition somewhere in the
middle instead, so the "while True:" works better.

>> This is very counter intuitive. e.g. range(1,4) returns
>> [1,2,3]. Better to have the default base as one rather than zero IMO. Of
>> course, the programmer should always be able to define the lower bound.
>> This cannot be changed, of course.
>
> It is true that starting counting at zero takes a bit of getting used
> to, and there are times when it is more convenient to start at one. But
> no one solution is ideal all the time, and we have to pick one system or
> the other, or else we end up with an overly complex syntax with marginal
> utility.

I've had to deal with mixed zero-based and one-based in coding before
(and still often), and it was a huge pain. Zero-based works much
better for many practical programming tasks, but can be difficult to
get used to. I work in game development, so it is not uncommon to have
mixed (artists and designers like one-based as they are generally
non-technical). It is pretty annoying when may variable names have to
be post fixed by "number" or "index" to try to keep it straight (and
that fails half the time). The worst I had to deal with regarding it
was in code that was crossing C++ and LUA, where C++ is zero-based and
LUA is one-based - it was extremely difficult to remember all the
needed +1s and -1s in the boundary code...

>>     Lack of a single character in a method to refer to an attribute
>> instead of a local variable, similar to C's "*" for dereferencing a pointer
>
> The lack of single-character syntax for many things helps prevents
> Python code looking like line noise.

I would not recommend this, however it should be noted that Python
assigns no special meaning to the name "self", and the variable could
be named anything you want in your methods, including single character
names. You would still need to type a minimum of two characters
though.

This is perfectly valid Python code, though would be against many (if
not most) style guides:

class MyObject:
    def __init__(s, myArg1, myArg2):
        s.myArg1 = myArg1
        s.myArg2 = myArg2

a = MyObject(1, 2)
print(a.myArg1, a.myArg2) # prints "1 2"

If the variable "s" were renamed to "self", you would get the exact
same result, however the code would match most style guides, and
linters will likely stop complaining.


More information about the Python-ideas mailing list