Pernyataan penetapan dalam Python tidak menyalin objek, mereka membuat ikatan antara target dengan sebuah objek. Untuk collections yang dapat diubah atau memiliki item-item yang dapat diubah, sebuah penyalinan terkadang dibutuhkan sehingga seseorang dapat mengubah salinan tanpa mengubah yang lainnya. Modul ini menyediakan operasi salinan dangkal dan salinan dalam (dijelaskan dibawah ini).
Ringkasan antarmuka:
copy.copy(x)Mengembalikan shallow copy dari x.
copy.deepcopy(x[, memo])Mengembalikan deep copy dari x.
exception copy.ErrorDi angkat untuk error spesifik dari modul.
Perbedaan antara shallow copy dan deep copy hanya relevan untuk objek majemuk (objek yang berisi objek lainnya, seperti list atau kelas):
Sebuah shallow copy membangun sebuah objek majemuk baru dan kemudian (sejauh mungkin) menyisipkan referensi ke dalamnya ke objek yang ditemukan di aslinya.
Sebuah deep copy membangun objek majemuk baruk dan kemudian, secara rekursif, menyisipkan salinan ke dalamnya ke objek yang ditemukan di aslinya.
Dua masalah yang sering muncul pada operasi deep copy yang tidak muncul pada operasi shallow copy:
Objek rekursif (objek majemuk yang secara langsung atau tidak langsung berisi sebuah referensi ke dirinya sendiri) mungkin menyebabkan sebuah perulangan rekursif.
Karena deep copy menyalin segalanya sehingga mungkin menyalin terlalu banyak, seperti data yang dimaksudkan untuk dibagikan kepada salinan lain.
Fungsi menghindari masalah diatas dengan:
menyimpan sebuah memo kamus dari objek yang sudah disalin selama penyalinan saat ini; dan
membiarkan kelas-kelas yang didefinisikan oleh user menimpa operasi penyalinan atau kumpulan komponen yang disalin.
This module does not copy types like module, method, stack trace, stack frame, file, socket, window, or any similar types. It does "copy" functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the module.
Salinan dangkal dari kamus dapat di buat menggunakan , dan list dengan menetapkan potongan dari keseluruhan list, sebagai contoh, copy.1.
Kelas-kelas dapat menggunakan antarmuka yang sama untuk mengontrol penyalinan yang mereka gunakan untuk mengontrol pengawetan. Lihat deskripsi dari modul untuk informasi dari metode ini. Faktanya, modul menggunakan fungsi pengawetan yang terdaftar dari modul .
In order for a class to define its own copy implementation, it can define special methods copy.5 and copy.6. The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the copy.6 implementation needs to make a deep copy of a component, it should call the function with the component as first argument and the memo dictionary as second argument. The memo dictionary should be treated as an opaque object.
The module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line.
See also
Coverage.pyA popular third-party coverage tool that provides HTML output along with advanced features such as branch coverage.
Command-Line Usage
The module can be invoked from the command line. It can be as simple as
python -m trace --count -C . somefile.py ...
The above will execute somefile.py and generate annotated listings of all Python modules imported during the execution into the current directory.
Display usage and exit.
--versionDisplay the version of the module and exit.
New in version 3.8: Added --module option that allows to run an executable module.
Main options
At least one of the following options must be specified when invoking . The option is mutually exclusive with the and options. When is provided, neither nor are accepted, and vice versa.
-c, --countProduce a set of annotated listing files upon program completion that shows how many times each statement was executed. See also , and below.
-t, --traceDisplay lines as they are executed.
-l, --listfuncsDisplay the functions executed by running the program.
-r, --reportProduce an annotated list from an earlier program run that used the and option. This does not execute any code.
-T, --trackcallsDisplay the calling relationships exposed by running the program.
Modifiers
-f, --file=<file>Name of a file to accumulate counts over several tracing runs. Should be used with the option.
-C, --coverdir=<dir>Directory where the report files go. The coverage report for trace0 is written to file trace1.
-m, --missingWhen generating annotated listings, mark lines which were not executed with trace2.
When using or , write a brief summary to stdout for each file processed.
-R, --no-reportDo not generate annotated listings. This is useful if you intend to make several runs with , and then produce a single set of annotated listings at the end.
-g, --timingPrefix each line with the time since the program started. Only used while tracing.
Filters
These options may be repeated multiple times.
--ignore-module=<mod>Ignore each of the given module names and its submodules (if it is a package). The argument can be a list of names separated by a comma.
--ignore-dir=<dir>Ignore all modules and packages in the named directory and subdirectories. The argument can be a list of directories separated by .
Programmatic Interface
class trace.Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None, timing=False)Create an object to trace execution of a single statement or expression. All parameters are optional. count enables counting of line numbers. trace enables line execution tracing. countfuncs enables listing of the functions called during the run. countcallers enables call relationship tracking. ignoremods is a list of modules or packages to ignore. ignoredirs is a list of directories whose modules or packages should be ignored. infile is the name of the file from which to read stored count information. outfile is the name of the file in which to write updated count information. timing enables a timestamp relative to when tracing was started to be displayed.
run(cmd)Execute the command and gather statistics from the execution with the current tracing parameters. cmd must be a string or code object, suitable for passing into .
runctx(cmd, globals=None, locals=None)Execute the command and gather statistics from the execution with the current tracing parameters, in the defined global and local environments. If not defined, globals and locals default to empty dictionaries.
runfunc(func, /, *args, **kwds)Call func with the given arguments under control of the object with the current tracing parameters.
results()Return a object that contains the cumulative results of all previous calls to trace0, trace1 and trace2 for the given instance. Does not reset the accumulated trace results.
class trace.CoverageResultsA container for coverage results, created by . Should not be created directly by the user.
update(other)Merge in data from another object.
write_results(show_missing=True, summary=False, coverdir=None)Write coverage results. Set show_missing to show lines that had no hits. Set summary to include in the output the coverage summary per module. coverdir specifies the directory into which the coverage result files will be output. If trace6, the results for each source file are placed in its directory.