Web to py enterprise web framework - p 6

LAMBDA 35 1 2 file = open('', 'w') ('hello world') Similarly, you can read back from the file with: 1 2 3 file = open('', 'r') print () hello world Alternatively, you can read in binary mode with "rb", write in binary mode with "wb", and open the file in append mode "a", using standard C notation. The read command takes an optional argument, which is the number of bytes. You can also jump to any location in a file using seek. You can read back from the file with read 1 2 3 print (6) print () world and you can close the. | LAMBDA 35 1 2 1 2 3 1 2 3 1 1 2 3 file open w hello world Similarly you can read back from the file with file open r print hello world Alternatively you can read in binary mode with rb write in binary mode with wb and open the file in append mode a using standard C notation. The read command takes an optional argument which is the number of bytes. You can also jump to any location in a file using seek. You can read back from the file with read print 6 print world and you can close the file with although often this is not necessary because a file is closed automatically when the variable that refers to it goes out of scope. When using WEB2py you do not know where the current directory is because it depends on how WEB2py is configured. The variable request. folder contains the path to the current application. Paths can be concatenated with the command os. path .join discussed below. lambda There are cases when you may need to dynamically generate an unnamed function. This can be done with the lambda keyword a lambda b b 2 print a 3 5 The expression lambda a b literally reads as a function with arguments a that returns b . Even if the function is unnamed it can be stored into a variable and thus it acquires a name. Technically this is different than using def because it is the variable referring to the function that has a name not the function itself. Who needs lambdas Actually they are very useful because they allow to refactor a function into another function by setting default arguments without defining an actual new function but a temporary one. For example 36 THE PYTHON LANGUAGE 1 2 3 4 1 2 3 4 5 1 1 1 1 2 3 4 5 def f a b return a b g lambda a f a 3 g 2 5 Here is a more complex and more compelling application. Suppose you have a function that checks whether its argument is prime def isprime number for p in range 2 number if number p return False return True This function is obviously .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
7    59    2    26-04-2024
127    251    4    26-04-2024
52    134    2    26-04-2024
92    340    2    26-04-2024
Đã 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.