3.10 Stack diagrams - sơ đồ xếp chồng

To keep track of which variables can be used where, it is sometimes useful to draw a stack diagram.
Like state diagrams, stack diagrams show the value of each variable, but they also show the function
each variable belongs to.


Each function is represented by a frame. A frame is a box with the name of a function beside it and
the parameters and variables of the function inside it. The stack diagram for the previous example
looks like this:


Để theo dõi các biến nào có thể được sử dụng ở đâu, đôi khi hữu ích để vẽ sơ đồ ngăn xếp. Giống như sơ đồ trạng thái, biểu đồ ngăn xếp hiển thị giá trị của mỗi biến, nhưng chúng cũng hiển thị chức năng của từng biến.

Mỗi hàm được biểu diễn bởi một khung. Khung là một hộp với tên của một hàm bên cạnh nó và các tham số và các biến của hàm bên trong nó. Biểu đồ ngăn xếp cho ví dụ trước đây sẽ như sau:





The frames are arranged in a stack that indicates which function called which, and so on. In this
example, print_twice was called by cat_twice, and cat_twice was called by __main__, which
is a special name for the topmost frame. When you create a variable outside of any function, it
belongs to __main__.


Các khung được sắp xếp trong một ngăn xếp chỉ ra chức năng nào được gọi là cái nào, vân vân. Trong ví dụ này, print_twice đã được gọi bởi cat_twice, và cat_twice đã được gọi bởi __main__, đây là một tên đặc biệt cho khung trên cùng. Khi bạn tạo một biến bên ngoài bất kỳ hàm nào, nó thuộc về __main__.

Each parameter refers to the same value as its corresponding argument. So, part1 has the same
value as line1, part2 has the same value as line2, and bruce has the same value as cat.
If an error occurs during a function call, Python prints the name of the function, and the name of the
function that called it, and the name of the function that called that, all the way back to __main__.


Mỗi tham số đề cập đến giá trị tương tự như đối số tương ứng của nó. Vì vậy, part1 có cùng giá trị với line1, part2 có cùng giá trị với line2, và bruce có cùng giá trị với cat.
Nếu một lỗi xảy ra trong một cuộc gọi chức năng, Python in tên của hàm, và tên của hàm gọi nó, và tên của hàm gọi đó, tất cả các trở về __main__.

For example, if you try to access cat from within print_twice, you get a NameError:
Traceback (innermost last):
File "test.py", line 13, in __main__
cat_twice(line1, line2)
File "test.py", line 5, in cat_twice
24 Chapter 3. Functions
print_twice(cat)
File "test.py", line 9, in print_twice
print(cat)
NameError: global name 'cat' is not defined
This list of functions is called a traceback. It tells you what program file the error occurred in, and
what line, and what functions were executing at the time. It also shows the line of code that caused
the error.
The order of the functions in the traceback is the same as the order of the frames in the stack diagram.
The function that is currently running is at the bottom


3.11 Fruitful functions and void functions

Some of the functions we are using, such as the math functions, yield results; for lack of a better
name, I call them fruitful functions. Other functions, like print_twice, perform an action but
don’t return a value. They are called void functions.

Một số chức năng chúng ta đang sử dụng, chẳng hạn như các hàm toán học, kết quả yield; vì thiếu một cái tên tốt hơn, tôi gọi họ là những hàm hiệu quả. Các chức năng khác, như print_twice, thực hiện một hành động nhưng không trả lại giá trị. Chúng được gọi là các hàm vô hiệu.

When you call a fruitful function, you almost always want to do something with the result; for
example, you might assign it to a variable or use it as part of an expression:

Khi bạn gọi một chức năng hiệu quả, bạn hầu như luôn luôn muốn làm điều gì đó với kết quả; ví dụ, bạn có thể gán nó cho một biến hoặc sử dụng nó như một phần của một biểu thức

x = math.cos(radians)
golden = (math.sqrt(5) + 1) / 2
When you call a function in interactive mode, Python displays the result:
>>> math.sqrt(5)
2.2360679774997898
But in a script, if you call a fruitful function all by itself, the return value is lost forever!
math.sqrt(5)
This script computes the square root of 5, but since it doesn’t store or display the result, it is not very
useful.
Void functions might display something on the screen or have some other effect, but they don’t have
a return value. If you try to assign the result to a variable, you get a special value called None.
Tính toán căn bậc 2 của 5, nhưng nó không lưu chữ hoặc hiển thị kết quả , nó không thực sự hữu ích.
Hàm vô hiệu có thể hiện thị một vài thứ trên màn hình hoặc có 1 vài tác dụng nhưng nó không trả lại giá trị. Nếu bạn thử gán một kết quả cho một biến, bạn sẽ nhận được giá trị đặc biệt gọi là None.

>>> result = print_twice('Bing')
Bing
Bing
>>> print(result)
None
The value None is not the same as the string 'None'. It is a special value that has its own type:
>>> print(type(None))
<class 'NoneType'>

The functions we have written so far are all void. We will start writing fruitful functions in a few

chapters


hàm mà chúng ta viết cho tới bây giờ đều là vô con mẹ nó dụng. Chúng ta sẽ bắt đầu viết hàm hữu dụng trong 1 số chương tới.

Nhận xét

Bài đăng phổ biến từ blog này

2.7. Operators and Operands - toán tử và toán hạng

1.11. Formal and Natural Languages