Đang chuẩn bị liên kết để tải về tài liệu:
Web to py enterprise web framework - p 6

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

LAMBDA 35 1 2 file = open('myfile.txt', 'w') file.write('hello world') Similarly, you can read back from the file with: 1 2 3 file = open('myfile.txt', 'r') print file.read() 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 file.seek(6) print file.read() world and you can close the. | LAMBDA 35 1 2 1 2 3 1 2 3 1 1 2 3 file open myfile.txt w file.write hello world Similarly you can read back from the file with file open myfile.txt r print file.read 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 file.seek 6 print file.read world and you can close the file with file.close 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. 2.14 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 .