tailieunhanh - PROGRAMMING IN PYTHON 3 - PART 7

Tham khảo tài liệu 'programming in python 3 - part 7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 360 Chapter 8. Advanced Programming Techniques Dictionary comprehensions 134 In debug mode the global variable_debug_is True. If this is the case we set up logging using the logging module and then create the @logged decorator. The logging module is very powerful and flexible it can log to files rotated files emails network connections HTTP servers and more. Here we ve used only the most basic facilities by creating a logging object setting its logging level several levels are supported and choosing to use a file for the output. The wrapper s code begins by setting up the log string with the function s name and arguments. We then try calling the function and storing its result. If any exception occurs we store it. In all cases the finally block is executed and there we add the return value or exception to the log string and write to the log. If no exception occurred the result is returned otherwise we reraise the exception to correctly mimic the original function s behavior. If Python is running in optimized mode debug_is False in this case we define the logged function to simply return the function it is given so apart from the tiny overhead of this indirection when the function is first created there is no runtime overhead at all. Note that the standard library s trace and cProfile modules can run and analyse programs and modules to produce various tracing and profiling reports. Both use introspection so unlike the @logged decorator we have used here neither trace nor cProfile requires any source code changes. Function Annotations Functions and methods can be defined with annotations expressions that can be used in a function s signature. Here s the general syntax def functionName par1 expl par2 exp2 . parN expN - rexp suite Every colon expression part expX is an optional annotation and so is the arrow return expression part - rexp . The last or only positional parameter if present can be of the form args with or without an annotation similarly the last or .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.