
Common mistakes made by beginner Python programmers - gilad
https://dev.to/anuragrana/5-common-mistakes-made-by-beginner-python-programmers-5ggp
======
brianberns
I’m not a Python fan and these examples reinforce my dislike for its design.

1\. Caused by confusing reference semantics and value semantics, which is
pretty much inevitable when a fundamental data type is mutable.

2\. Boolean is a subclass of Integer, because... why?

3\. Same problem as #1.

4\. Once again, beginners have no understanding of reference vs. value
semantics, and why should they?

5\. Default arguments of a function are evaluated just once, because... why?
This one just seems crazy to me. It turns default arguments into stateful
globals, so an innocent-looking function suddenly exhibits unpredictable
behavior.

~~~
wodenokoto
I sum my booleans all the time in python, but other than that I agree.

This whole mutating or not mutating thing was a huge headache when starting
python and still causes gotchas.

R is supposedly the “poorly designed” programming language that data
scientists may use instead of python, but I’ve never had such problems in R.

~~~
tempguy9999
> This whole mutating or not mutating thing was a huge headache when starting
> python and still causes gotchas.

Well, I'd say 1) immutability is expensive[0] and 2) mutability is sometimes
simpler and clearer than immutability for some algorithms IMO. So I'd want
python to have both.

Today's passion for 'immutable everything' is going to burn out pretty quick
as it's great but being used too much and without recognising its cost. IMO
again.

[0] if naively implemented, and it so often is.

~~~
wodenokoto
R doesn’t have strict immutability. I’m not sure which data structures in R
are mutable, but I’m sure it’s most, as a common operation is to change the
values of a subset of a list/vector/dataframe.

So my point is not “python is hard because of mutability” it’s that pythons
mutability really gets in the way of the beginner and causes lots of gotchas
for even well experienced developers.

~~~
tempguy9999
> So my point is not “python is hard because of mutability”...

ok

> ...it’s that pythons mutability really gets in the way of the beginner and
> causes lots of gotchas for even well experienced developers

So python _is_ hard because of mutability? (well, harder anyway).

The fact is, if python had immutability it'd be semantically simpler.
Unquestionably (I'd claim). But that comes at a performance cost, so
mutability is allowed. That's when we get these confusions of what an
operation does (because the semantics get non-simple). My point is either way
we pay a price.

Anyway I have a feeling I may be misconstruing your point, sorry if that's so.

------
Smithalicious
Allow me to play devil's advocate for a bit:

1\. I don't really think this is that confusing. It's just passing by
reference, which is something you can't get around learning if you're using a
language that works that way.

2\. I don't really see why True == 1 should hold either, but I think this
example is really contrived; you wouldn't make a dictionary with both integer
and boolean keys.

3\. These are just destructive methods. You could get around this particular
example by making these methods return the object itself, but then the user
would just continue to operate under the misunderstanding that these methods
are non-destructive, which would come back to bite them later.

4\. This is an implementation detail leaking, but I can't think of a scenario
where this shows up in the real world. To begin with, I rarely use "is"
anyways.

5\. This one is indeed just very confusing and I can't really defend it. This
is the only one that I think has a real chance of being a problem in the real
world. I know I've been bitten by this one before.

I think most of these just boil down to "mutability is confusing". Sure, but
that's not really something you can fix unless you want to do away with
mutability altogether, and then you have to eat the performance cost.

------
wvlia5
This python program is amazing:

exec(''.join(chr(int(''.join(str(ord(i)-8203)for i in c),2))for c in '
'.split(' ')))

