html tool

2013年3月17日星期日

Dive into Python 的我的翻译 XXXVI------8.5 VII

Example 8.10. Introducing locals
  • >>> def foo(arg): -----------------------------1
    ...     x = 1
    ...     print locals()
    ...    
    >>> foo(7)--------------------------------------2
    {'arg': 7, 'x': 1}
    >>> foo('bar')--------------------------------------3
    {'arg': 'bar', 'x': 1}
    • 1
      [原文]P110
      The function foo has two variables in its local namespace: arg, whose value is passed in to the function, and x, which is defined within the function.
      [pope译]
      函数foo在它的本地命名空间中有两个变量:arg,它的值是传入函数的参数;x,函数中定义的
      [net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html]
      函数 foo 在它的局部名字空间中有两个变量:arg (它的值是被传入函数的) 和 x (它是在函数里定义的)。
      2
      [原文]P110
      locals returns a dictionary of name/value pairs. The keys of this dictionary are the names of the variables as strings; the values of the dictionary are the actual values of the variables. So calling foo with 7 prints the dictionary containing the function's two local variables: arg (7) and x (1).
      [pope译]
      locals f返回 名称/值 配对的字典内容。字典的主键是字符变量的名称,字典的值是变量实际{actual}的值.所以调用foo传入7打印此函数的两个本地变量是:arg(7)和x(1)
      [net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html]
      locals 返回一个名字/值对的 dictionary。这个 dictionary 的键字是字符串形式的变量名字,dictionary 的值是变量的实际值。所以用 7 来调用 foo,会打印出包含函数两个局部变量的 dictionary:arg (7) 和 x (1)
      3
      [原文] P110
      Remember, Python has dynamic typing, so you could just as easily pass a string in for arg; the function (and the call to locals) would still work just as well. locals works with all variables of all datatypes.
      [pope译]
      记得吧!Python是动态类型变量,所以你也可以只简单的传给arg一个字符串,这个函数(同时调用locals)仍然可以正常工作。locals 可以在全部的数据类型变量下工作
      [net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html]
      回想一下,Python 有动态数据类型,所以您可以非常容易地传递给 arg 一个字符串,这个函数 (和对 locals 的调用) 将仍然很好的工作。locals 可以用于所有类型的变量

没有评论:

发表评论