# 12.7 变量获取

引用一个变量的最常用方法是使用名称对应的符号，详情查阅 符号表达式。

不过有时，你可能想引用仅在运行时决定的变量。这时，你就不能简单的使用变量名对应的符号来进行引用。你需要使用 symbol-value 函数来获取这个值。

Function: symbol-value symbol\
&#x20; 这个函数返回储存在符号 symbol 的 value 部分的值。而这个值是变量当前（动态绑定）的值。如果这个变量没有局部绑定，那么就直接使用它的全局绑定。如果这个变量是无效的，那么则会抛出 void-variable 错误。\
&#x20; 如果变量是词法绑定的，那么使用 symbol-value 函数获取的值和该变量语法值可能会不同。后者是由语法环境而不是符号 value 部分决定的。详情查阅 变量作用域章

```
(setq abracadabra 5)
    => 5
(setq foo 9)
    => 9

;; 这里，符号 abracadabra 被当作 symbol 检索
(let ((abracadabra 'foo))
  (symbol-value 'abracadabra))
    => foo
    
;; 这里，符号 abracadabra 的 value， 也就是 foo
;; 被当作 symbol 检索
(let ((abracadabra 'foo))
  (symbol-value abracadabra))
    => 9

(symbol-value 'abracadabra)
    => 5
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emacs-lisp.ivory.cafe/bian-liang/untitled-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
