>>> element = 'silver'
>>> number = 47
>>> 'Element {number} is {element}'.format(**locals())
'Element 47 is silver'

这里通过 映射操作符'**'将当前还在操作范围内的element,number传递给函数locals()

locals()函数访问当前还在作用范围内的局部变量的更多相关文章

  1. Python命名空间——locals()函数和globals()函数及局部赋值规则

    Python使用叫做命名空间的东西来记录变量的轨迹.命名空间只是一个 字典,它的键字就是变量名,字典的值就是那些变量的值.实际上,命名空间可以象Python的字典一样进行访问,一会我们就会看到. 在一 ...

  2. globals() 和 locals() 函数

    globals() 和 locals() 函数 根据调用地方的不同,globals() 和 locals() 函数可被用来返回全局和局部命名空间里的名字. 如果在函数内部调用 locals(),返回的 ...

  3. Python locals() 函数

    Python locals() 函数  Python 内置函数 描述 locals() 函数会以字典类型返回当前位置的全部局部变量. 对于函数, 方法, lambda 函式, 类, 以及实现了 __c ...

  4. C++中函数访问数组的方式

    在书写C++代码时,往往为了令代码更加简洁高效.提高代码可读性,会对定义的函数有一些特殊的要求:比如不传递不必要的参数,以此来让函数的参数列表尽可能简短. 当一个函数需要访问一个数组元素时,出于上述原 ...

  5. Python globals和locals函数_reload函数

    Python globals和locals函数_reload函数: globals( ): 返回所有能够访问到的全局名字 num = 5 sum = 0 def add(num): func_sum ...

  6. day03 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  7. Python的locals()函数

    Python的locals()函数会以dict类型返回当前位置的全部局部变量. 示例代码: def func(): arg_a, arg_b = 'a', 'b' def func_a(): pass ...

  8. 阿里云API网关(11)外网访问 阿里云API网关内定义的API步骤:

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  9. Django 使用 locals() 函数

    locals() 函数会以字典类型返回当前位置的全部局部变量. 在 views.py 中添加 from django.shortcuts import render,HttpResponse,rend ...

随机推荐

  1. Java小陷阱

    基本数据类型与字符串的连接 在Java中,+不仅可作为加法运算符使用,还可作为字符串连接运算符使用. 当把任何基本数据类型的值与字符串值进行连接运算时,基本类型的值将自动类型转换为字符串类型. pub ...

  2. 转:vs发布window应用程序时出错:未能签名 ...\setup.exe

      在vs发布window应用程序过程中,提示“SignTool 报告了一个错误‘未能签名 ...\setup.exe.SignTool 错误: ISignCode::Sign 返回的错误: 0x80 ...

  3. 通过Profiles查看create语句的执行时间消耗 (转)

    一,查看profiles的状态值   1,查看profiles是否已经打开了,默认是不打开的.   mysql> show profiles;   Empty set (0.02 sec) my ...

  4. 【linux】find命令详解

    find命令格式:find [搜索范围][匹配条件] -name 参数:按照名字查找 [root@andon ~]# find /root -name test ###精确查找 /root/test ...

  5. SPOJ #5 The Next Palindrome

    "not more than 1000000 digits" means an efficient in-place solution is needed. My first so ...

  6. 剑指offer系列49--求1+2+...+N的和

    [题目]求1+2+3+…+n, * 要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). package com.exe10.offer ...

  7. Configure custom SSL certificate for RDP on Windows Server 2012 in Remote Administration mode

    Q: So the release of Windows Server 2012 has removed a lot of the old Remote Desktop related configu ...

  8. Env:Winmanager插件使用

    转自:http://www.cnblogs.com/feichexia/archive/2012/11/06/vim_WinManager.html 1.准备winmanger插件,从下面网址下即可: ...

  9. PHP解码unicode编码中文字符代码示例

    在抓取某网站数据,结果在数据包中发现了一串编码的数据:"......\u65b0\u6d6a\u5fae\u535a......", 这其实是中文被unicode编码后了的数据,想 ...

  10. (C/C++) Interview in English - Basic concepts.

    Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...