Programming FAQ — Python 3.16.0a0 documentation

docs.python.org

Tin mới

General Python FAQ

Design and History FAQ

Python Module Index

3.16.0a0 Documentation

Python Frequently Asked Questions

Is there a source code-level debugger with breakpoints and single-stepping?¶

Several debuggers for Python are described below, and the built-in function breakpoint() allows you to drop into any of them.

documented in the Library Reference Manual

Several debuggers for Python are described below, and the built-in function breakpoint() allows you to drop into any of them.

idlelib: Implementation package for the IDLE shell/editor.

Several debuggers for Python are described below, and the built-in function breakpoint() allows you to drop into any of them.

Why am I getting an UnboundLocalError when the variable has a value?¶

It can be a surprise to get the UnboundLocalError in previously working code when it is modified by adding an assignment statement somewhere in the body of a function.

Why am I getting an UnboundLocalError when the variable has a value?¶

It can be a surprise to get the UnboundLocalError in previously working code when it is modified by adding an assignment statement somewhere in the body of a function.

sys: Access system-specific parameters and functions.

standard library modules – such as sys, os, argparse, re

os: Miscellaneous operating system interfaces.

standard library modules – such as sys, os, argparse, re

argparse: Command-line option and argument parsing library.

standard library modules – such as sys, os, argparse, re

re: Regular expression operations.

standard library modules – such as sys, os, argparse, re

What is the difference between arguments and parameters?¶

Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what kind of arguments a function can accept. Fo

Why did changing list ‘y’ also change list ‘x’?¶

you might be wondering why appending an element to y changed x too.

Why did changing list ‘y’ also change list ‘x’?¶

you might be wondering why appending an element to y changed x too.

How do I copy an object in Python?¶

In general, try copy.copy() or copy.deepcopy() for the general case. Not all objects can be copied, but most can.

How do I convert a number to a string?¶

For example, to convert the number 144 to the string '144', use the built-in type constructor str(). If you want a hexadecimal or octal representation, use the built-in functions hex() or oct(). For fancy formatting, see

Format string syntax

For example, to convert the number 144 to the string '144', use the built-in type constructor str(). If you want a hexadecimal or octal representation, use the built-in functions hex() or oct(). For fancy formatting, see

How do I modify a string in place?¶

You can’t, because strings are immutable. In most situations, you should simply construct a new string from the various parts you want to assemble it from. However, if you need an object with the ability to modify in-pla

array: Space efficient arrays of uniformly typed numeric values.

You can’t, because strings are immutable. In most situations, you should simply construct a new string from the various parts you want to assemble it from. However, if you need an object with the ability to modify in-pla

What does UnicodeDecodeError or UnicodeEncodeError error mean?¶

Can I end a raw string with an odd number of backslashes?¶

A raw string ending with an odd number of backslashes will escape the string’s quote:

profile: Pure Python profiler (deprecated). (deprecated)

You should always find the hot spots in your program before attempting to optimize any code (see the profile module).

timeit: Measure the execution time of small code snippets.

Writing benchmark scripts will allow you to iterate quickly when searching for improvements (see the timeit module).

collections: Container datatypes

Use the right data structures. Study documentation for the Built-in Types and the collections module.

Sorting Techniques

write a C extension module

That’s a tough one, in general. First, here is a list of things to remember before diving further:

Time complexity of operations on built-in types

str and bytes objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string l

How do I apply a method or function to a sequence of objects?¶

To call a method or function and accumulate the return values in a list, a list comprehension is an elegant solution:

Why does a_tuple[i] += [‘item’] raise an exception when the addition works?¶

This is because of a combination of the fact that augmented assignment operators are assignment operators, and the difference between mutable and immutable objects in Python.

My class defines __del__ but it is not called when I delete the object.¶

The del statement does not necessarily call __del__() – it simply decrements the object’s reference count, and if this reaches zero __del__() is called.

weakref: Support for weak references and weak dictionaries.

The del statement does not necessarily call __del__() – it simply decrements the object’s reference count, and if this reaches zero __del__() is called.

@functools.cached_property

The two principal tools for caching methods are @functools.cached_property and @functools.lru_cache. The former stores results at the instance level and the latter at the class level.

PYTHONDONTWRITEBYTECODE

One reason that a .pyc file may not be created is a permissions problem with the directory containing the source file, meaning that the __pycache__ subdirectory cannot be created. This can happen, for example, if you dev

py_compile: Generate byte-code files from Python source files.

One reason that a .pyc file may not be created is a permissions problem with the directory containing the source file, meaning that the __pycache__ subdirectory cannot be created. This can happen, for example, if you dev

compileall: Tools for byte-compiling all Python source files in a directory tree.

One reason that a .pyc file may not be created is a permissions problem with the directory containing the source file, meaning that the __pycache__ subdirectory cannot be created. This can happen, for example, if you dev

importlib.import_module

Consider using the convenience function import_module() from importlib instead: