Tin mới
Python documentation for the current stable release
Is there a source code level debugger with breakpoints, single-stepping, etc.?
Python Frequently Asked Questions
Is there a source code level debugger with breakpoints, single-stepping, etc.?¶
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.
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.
What are the “best practices” for using import in a module?¶
In general, don’t use from modulename import *. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.
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 types of arguments a function can accept. F
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 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 string to a number?¶
For integers, use the built-in int() type constructor, e.g. int('144') == 144. Similarly, float() converts to floating-point, e.g. float('144') == 144.0.
To convert, e.g., 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 the F
To convert, e.g., 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 the F
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?¶
profile: Python source profiler.
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.
That’s a tough one, in general. First, here are a list of things to remember before diving further:
Delegation is an object oriented technique (also called a design pattern). Let’s say you have an object x and want to change the behaviour of just one of its methods. You can create a new class that provides a new implem
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.
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
Consider using the convenience function import_module() from importlib instead: