How-to articles, tricks, and solutions about PYTHON

What do __init__ and self do in Python?

__init__ is a special method in Python classes, also known as a constructor.

What does __all__ mean in Python?

In Python, __all__ is a list of strings that defines the names that should be imported when from <module> import * is used.

What does -> mean in Python function definitions?

In Python, the "->" symbol is used to indicate the return type of a function.

What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?

The "SyntaxError: Missing parentheses in call to 'print'" error message is raised when you are using Python 3 and you have forgotten to include the parentheses when calling the print() function.

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

In Python, the double star (**) is used to denote an "unpacking" operator, which allows you to unpack a dictionary or other iterable data type into keyword arguments in a function call.

What does %s mean in a Python format string?

In a Python format string, the %s placeholder represents a string.

What does functools.wraps do?

functools.wraps is a decorator that can be used to modify a function or method by updating its metadata.

What does if __name__ == "__main__": do?

The special __name__ variable in Python is a string that contains the name of the current module. If the module is the main program, __name__ will be set to the string "__main__".

What does the 'b' character do in front of a string literal?

The 'b' character in front of a string literal indicates that the string is a bytes literal.

What does the "at" (@) symbol do in Python?

In Python, the "at" (@) symbol is used to decorate a function.

What does the "yield" keyword do?

Python, the yield keyword is used in the body of a function like a return statement, but instead of returning a value and terminating the function, it yields a value and suspends the function's execution.

What IDE to use for Python?

There are many Integrated Development Environments (IDEs) that you can use for writing, testing, and debugging Python code.

What is __future__ in Python used for and how/when to use it, and how it works

The __future__ module in Python allows you to enable new language features which are not compatible with the current version of Python.

What is __init__.py for?

__init__.py is a special Python file that is used to indicate that the directory it is present in is a Python package.

What is __pycache__?

__pycache__ is a directory that is created by the Python interpreter when it imports a module.

What is a clean "pythonic" way to implement multiple constructors?

A "pythonic" way to implement multiple constructors in Python is to use the @classmethod decorator.

What is a cross-platform way to get the home directory?

The os.path module in Python provides a cross-platform way to get the home directory.

What is a mixin and why is it useful?

A mixin in Python is a class that is used to add specific functionality to other classes without inheriting from them.

What is setup.py?

setup.py is a Python script used to build and install Python packages.

What is the best project structure for a Python application?

There is no one "best" project structure for a Python application, as it often depends on the specific requirements and goals of the project.

What is the difference between __str__ and __repr__?

__str__ and __repr__ are two special methods in Python classes.

What is the difference between dict.items() and dict.iteritems() in Python2?

In Python 2, dict.items() returns a list of the dictionary's key-value pairs, whereas dict.iteritems() returns an iterator over the dictionary's key-value pairs.