This is a list of common questions within the Python tag. Each question includes some canonical SO questions that can be used to close-vote any new questions that match. If you have any suggestions then please come see us in chat.

  • Each space separated value is a separate search term.
  • Terms are ANDed together.
  • [Brackets] indicate a tag term.
  • is:draft shows only draft posts.
  • is:community shows only community posts.
  • 101 - 137 of 137

A typo like list["abcd"] instead of list("abcd") leads to Type Error: “'types.GenericAlias' object is not ...”

You get this error when you try to call X() when X isn’t a function, method, or class.

A type hint that tries to use a not-yet-defined name will throw a NameError.

Most commonly, this is a Windows user forgetting to put encoding='utf-8' when opening a file.

User tries to read data from a file which is clearly not in UTF-8, but fails to understand that this is what the error message means.

When inside a Jinja expression, you reference the variables directly, rather than within nested curly braces.

When using str.join a list comprehension (rather than a generator expression) provides a significant speed boost

The splat operator (*) can be used to unpack variables from a container when calling a function.

A function using print for the output seems to return it, but does not allow using the output.

From Python 3 onwards reduce must be imported from functools.

% can be used in string formatting. Note that it is now recommended to use the new str.format() syntax.

Choosing elements from a sequence with a non-uniform probability.

__all__ defines the public objects within a module

How does splat unpacking work for sequences (*) and dictionaries (**).

Seeing something like @contract.bind indicates a decorator function in python.

A docstring is a the first unassigned string in a function, class, or module declaration.

The terminology makes finding a canonical “That’s a list / dict / etc. comprehension” question hard.

Why doesn’t my Python execute in this rendered JavaScript?

The representation of a string with backslashes can sometimes throw a user as they are escaped.

If a function does not return a value then it automatically returns None.

The recursive call in a function misses a return and thus sometimes returns None.

Python parses 2. as the float 2.0, this can cause troubles when trying to use methods on integers.

Testing against multiple values using boolean operators, sometimes people think booleans work like English grammar.

The OP expects a variable in their callback lambdas to have the value it had when the function was created, instead it has the current value at calling time.

Coming from other languages people might try to use && and || as logical operators, which fails. The obvious solution is to use & and | instead… and then it still “doesn’t work”, because they need and and or.

Strings are immutable, so methods on the string that alter the string return a new value. Typically beginners forget this and call str.upper() or str.lower() or str.title() or str.replace() or str.strip() without assigning the new string back to the variable.

It’s a hardware issue.

The way Python handles newlines on Windows can result in blank lines appearing between rows when using csv.writer.

  • 101 - 137 of 137