How-to articles, tricks, and solutions about PYTHON

TypeError: method() takes 1 positional argument but 2 were given

This error message is indicating that a method or function called "method" is expecting one argument, but it was called with two arguments.

TypeError: Missing 1 required positional argument: 'self'

This error message is indicating that a class method is being called without providing the "self" parameter, which is the first parameter for all class methods and refers to the instance of the class.

Understanding Python super() with __init__() methods

The super() function is a way to refer to the parent class and its attributes.

Understanding slicing

In Python, slicing refers to taking a subset of a sequence (such as a list, string, or tuple) by using indices to specify the start and end points of the slice.

Unicode (UTF-8) reading and writing to files in Python

To read a file in Unicode (UTF-8) encoding in Python, you can use the built-in open() function, specifying the encoding as "utf-8".

UnicodeDecodeError, invalid continuation byte

Here is an example of a Python code snippet that could cause a UnicodeDecodeError: invalid continuation byte error:

UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

This error occurs when trying to decode a string using the 'charmap' codec, which is typically used for Windows-1252 character encoding.

UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c

This error occurs when a file or string that is being decoded using the UTF-8 encoding contains an invalid byte sequence.

UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte

This error occurs when trying to decode a byte string using the UTF-8 codec and the byte at the given position is not a valid start byte for a UTF-8 encoded character.

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

This error is raised when trying to encode a Unicode string using the ASCII codec, and the string contains a character that is not within the ASCII range (0-127).

Unzipping files in Python

Here is a code snippet that demonstrates how to unzip a file using the zipfile module in Python:

Usage of __slots__?

__slots__ is a way to specify a fixed set of attributes for a class, which can help to save memory in certain situations.

Use a list of values to select rows from a Pandas dataframe

You can use the .loc property of a Pandas dataframe to select rows based on a list of values.

Use different Python version with virtualenv

To use a different Python version with virtualenv, follow these steps:

Use of *args and **kwargs

In Python, *args and **kwargs are special keywords that allow you to pass a variable number of arguments to a function.

Using global variables in a function

In Python, you can use global variables in a function by declaring the variable as global within the function.

Using Python 3 in virtualenv

Here is an example of how you can use Python 3 in a virtual environment using the virtualenv package:

ValueError: could not convert string to float: id

This error message is indicating that the program is trying to convert a string to a float, but the string is not a valid number.

ValueError: invalid literal for int() with base 10:

In this code snippet, the variable "value" is being set to the integer representation of the string "invalid." However, "invalid" is not a valid integer, so the int() function will raise a ValueError.

ValueError: setting an array element with a sequence

This code creates a 2D numpy array, and then tries to set the first element of the array to a list.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

This error message is indicating that a boolean operation was performed on an array with multiple elements, and the result of the operation is ambiguous.

What are "named tuples" in Python?

"Named tuples" in Python are a subclass of the built-in tuple type, but with the added ability to access elements by name rather than just by index.

What are metaclasses in Python?

In Python, a metaclass is a class that defines the behavior of a class. When you create a class, Python automatically creates a metaclass for you behind the scenes. You can think of a metaclass as a blueprint for creating a class.

What are the differences between type() and isinstance()?

In Python, type() is a built-in function that returns the type of an object, while isinstance() is a function that checks whether an object is an instance of a particular class or of a subclass thereof.