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) def
s and lambda
s, 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)
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)
python 2 :
import __builtin__
def sorted(services):
return __builtin__.sorted(services, key=lambda s: s.sortkey())
python 3 :
import builtins
def sorted(services):
return builtins.sorted(services, key=lambda s: s.sortkey())
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…