r/pythontips • • Oct 17 '21

Syntax Python Beginner Mistakes

As a Python trainer, I get to work with pynewbies across the spectrum. It doesn't matter whether they are from college or years into the industry when learning Python. The simplest language on the earth is not that intuitive. Things get even more challenging when they are well versed in a Typed-Object-Oriented-Language

I have compiled some of the common mistakes that beginners make.

https://thelearning.dev/python-beginner-mistakes

52 Upvotes

12 comments sorted by

27

u/[deleted] Oct 17 '21

As a Python trainer

Why did I go into this thread expecting snakes that could do tricks

5

u/R3D3-1 Oct 18 '21

Given the culture of euphemisms, the misunderstanding could have been worse.

5

u/[deleted] Oct 18 '21

My gf thanks you for her new nickname.

3

u/EyeofEnder Oct 18 '21

I've always used type() for built-in types and isinstance() for classes.

Is it better to just generally use isinstance() for type checking?

4

u/JRRudy Oct 18 '21

Yes, stick with isinstance() unless you have a specific reason not to. One key difference is that isinstance will allow subclasses. Say you have a function that takes in a dict as an argument. If you enforce the type using type() and the user inputs an object of a dict subclass such as OrderedDict, it will evaluate to False. This is rarely the desired behavior, as you should typically be able to do anything with an object of a subclass that you could do with an object of its superclass. Why restrict your function to specifically using a dict, when a subclass of dict will have all the methods and attributes it needs to play the part just as well?

3

u/R3D3-1 Oct 18 '21 edited Oct 18 '21

Coming from Javascript, one of my students was surprised to see no Ternary operators in Python.

But it has.

x = -1 if y < 0 else +1 if y > 0 else 0 // vs
x = y < 0 ? -1 : y > 0 ? +1 : 0;

The only unusual thing about Python's ternary expressions is that the expressions, which are not skipped, are not evaluated left-to-right. An aspect I personally dislike; I'd much prefer if python had opted for left-to-right expressions, e.g.

x = if y < 0 then -1 else if y > 0 then +1 else 0
l2 = [for item in l1 INVENTKEYWORDHERE f(item) ]

More verbose, but it would preserve consistency between control flow statements and the corresponding relations. Particularly obnoxious is to remember the order of item produced by

l3 = [(it1, it2) for it1 in l1 for it2 in l2]

partly due to flipping back and forth the ordering.

Write a function called middle that takes a list returns a new list that contains all but the first and last elements

The given examples actually even fail the specification; The first probably has a typo (it should probably be l1 in the argument list), otherwise it would have a name error, but even then both examples modify the existing list by side-effect.

An indexed tuple or list makes the code hard to read. Always unwrap it into proper variable names and use them.

Missed opportunity to write it as

for shape_name, length in shape_with_length:
    area(shape_name,length)

2

u/usnobl Oct 18 '21

Thank you! Bookmarking this

3

u/srpwnd Oct 17 '21

I think you should mention that you could (should) write the ternary operator code as: result = INVALID_INPUT if name == ‘’ or email == ‘’ else f”{name}\n{email}\n{designation}”

1

u/bazzam13 Oct 18 '21

Using Package names as file names

Whenever I introduce flask to my students, I name the file app.py, and there will be one person in the class who calls it flask.py that will give you an error like Flask not found in module flask.

The error occurs because the Python interpreter searches for the Flask class in the flask.py you created instead of the actual flask package.

Never name your module the same as the package you are trying to use.

100% did this the first time I tried to make a Flask app.

1

u/[deleted] Oct 24 '21

[removed] — view removed comment

1

u/bhavaniravi Oct 25 '21

Oh, trust me, even I thought the same before I started teaching. When I started teaching, I was surprised when people struggled with indentation, lists(over arrays), and not writing main functions.

One language cannot be intuitive for everyone. It's all about our pre-conditioning