<i id='lasE4'><tr id='lasE4'><dt id='lasE4'><q id='lasE4'><span id='lasE4'><b id='lasE4'><form id='lasE4'><ins id='lasE4'></ins><ul id='lasE4'></ul><sub id='lasE4'></sub></form><legend id='lasE4'></legend><bdo id='lasE4'><pre id='lasE4'><center id='lasE4'></center></pre></bdo></b><th id='lasE4'></th></span></q></dt></tr></i><div id='lasE4'><tfoot id='lasE4'></tfoot><dl id='lasE4'><fieldset id='lasE4'></fieldset></dl></div>

      <legend id='lasE4'><style id='lasE4'><dir id='lasE4'><q id='lasE4'></q></dir></style></legend>

      <tfoot id='lasE4'></tfoot>
    1. <small id='lasE4'></small><noframes id='lasE4'>

        <bdo id='lasE4'></bdo><ul id='lasE4'></ul>
      1. python函数可以调用同名的全局函数吗?

        时间:2023-09-10
          <i id='DHSIM'><tr id='DHSIM'><dt id='DHSIM'><q id='DHSIM'><span id='DHSIM'><b id='DHSIM'><form id='DHSIM'><ins id='DHSIM'></ins><ul id='DHSIM'></ul><sub id='DHSIM'></sub></form><legend id='DHSIM'></legend><bdo id='DHSIM'><pre id='DHSIM'><center id='DHSIM'></center></pre></bdo></b><th id='DHSIM'></th></span></q></dt></tr></i><div id='DHSIM'><tfoot id='DHSIM'></tfoot><dl id='DHSIM'><fieldset id='DHSIM'></fieldset></dl></div>

          <small id='DHSIM'></small><noframes id='DHSIM'>

        1. <tfoot id='DHSIM'></tfoot>
          <legend id='DHSIM'><style id='DHSIM'><dir id='DHSIM'><q id='DHSIM'></q></dir></style></legend>

              <bdo id='DHSIM'></bdo><ul id='DHSIM'></ul>

                <tbody id='DHSIM'></tbody>

                  本文介绍了python函数可以调用同名的全局函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我可以从同名函数调用全局函数吗?

                  Can I call a global function from a function that has the same name?

                  例如:

                  def sorted(services):
                      return {sorted}(services, key=lambda s: s.sortkey())
                  

                  {sorted} 我的意思是全局排序函数.有没有办法做到这一点?然后我想用模块名称调用我的函数:service.sorted(services)

                  By {sorted} I mean the global sorted function. Is there a way to do this? I then want to call my function with the module name: service.sorted(services)

                  我想使用相同的名称,因为它与全局函数做同样的事情,只是它添加了一个默认参数.

                  I want to use the same name, because it does the same thing as the global function, except that it adds a default argument.

                  推荐答案

                  Python 的名称解析方案有时被称为 LEGB 规则,这意味着当您在函数中使用非限定名称时,Python 最多搜索四个范围——首先本地 (L) 范围,然后是任何封闭 (E) deflambda 的本地范围s,然后是全局 (G) 范围,最后是内置 (B) 范围.(请注意,一旦找到匹配项,它将立即停止搜索)

                  Python's name-resolution scheme which sometimes is referred to as LEGB rule, implies that when you use an unqualified name inside a function, Python searches up to four scopes— First the local (L) scope, then the local scopes of any enclosing (E) defs and lambdas, then the global (G) scope, and finally the built-in (B) scope. (Note that it will stops the search as soon as it finds a match)

                  因此,当您在函数解释器中使用 sorted 时,会将其视为 全局 名称(您的函数名称),因此您将拥有一个递归函数.如果你想访问内置的 sorted 你需要为 Python 指定它.通过 __builtin__ 模块(在 Python-2.x 中)和 builtins 在 Python-3.x 中(此模块提供对 Python 的所有内置"标识符的直接访问)

                  So when you use sorted inside the functions interpreter considers it as a Global name (your function name) so you will have a recursion function. if you want to access to built-in sorted you need to specify that for Python . by __builtin__ module (in Python-2.x ) and builtins in Python-3.x (This module provides direct access to all ‘built-in’ identifiers of Python)

                  蟒蛇2:

                  import __builtin__
                  def sorted(services):
                      return __builtin__.sorted(services, key=lambda s: s.sortkey())
                  

                  蟒蛇3:

                  import builtins
                  def sorted(services):
                      return builtins.sorted(services, key=lambda s: s.sortkey())
                  

                  这篇关于python函数可以调用同名的全局函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:未绑定的变量和名称 下一篇:exec() 方法中自定义命名空间的子类化 python 字典

                  相关文章

                  最新文章

                  <tfoot id='FCKir'></tfoot>

                  1. <small id='FCKir'></small><noframes id='FCKir'>

                    <i id='FCKir'><tr id='FCKir'><dt id='FCKir'><q id='FCKir'><span id='FCKir'><b id='FCKir'><form id='FCKir'><ins id='FCKir'></ins><ul id='FCKir'></ul><sub id='FCKir'></sub></form><legend id='FCKir'></legend><bdo id='FCKir'><pre id='FCKir'><center id='FCKir'></center></pre></bdo></b><th id='FCKir'></th></span></q></dt></tr></i><div id='FCKir'><tfoot id='FCKir'></tfoot><dl id='FCKir'><fieldset id='FCKir'></fieldset></dl></div>
                    1. <legend id='FCKir'><style id='FCKir'><dir id='FCKir'><q id='FCKir'></q></dir></style></legend>
                      • <bdo id='FCKir'></bdo><ul id='FCKir'></ul>