当前位置:主页 > 软件编程 > Python代码 >

python关于调用函数外的变量实例

时间:2021-04-28 08:04:23 | 栏目:Python代码 | 点击:

实例如下所示:

class Solution(object):
  def foo(self, s):
    def bar(a):
      s += a
      print s
    bar("aa")
Solution().foo("ss")

运行结果

UnboundLocalError: local variable 's' referenced before assignment
class Solution(object):
  def foo(self, s):
    def bar(a):
      print s
    bar("aa")
Solution().foo("ss")

运行结果ss

您可能感兴趣的文章:

相关文章