函数名在python类中未定义

编程入门 行业动态 更新时间:2024-10-21 06:42:43
本文介绍了函数名在python类中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对 python 比较陌生,我在命名空间方面遇到了一些问题.

I am relatively new to python and i am experiencing some issues with namespacing.

class a: def abc(self): print "haha" def test(self): abc() b = a() b.test() #throws an error of abc is not defined. cannot explain why is this so

推荐答案

由于 test() 不知道谁是 abc,所以 msg NameError:全局名称 'abc' 未定义 您看到应该在调用 b.test() 时发生(调用 b.abc() 很好),更改它:

Since test() doesn't know who is abc, that msg NameError: global name 'abc' is not defined you see should happen when you invoke b.test() (calling b.abc() is fine), change it to:

class a: def abc(self): print "haha" def test(self): self.abc() # abc() b = a() b.abc() # 'haha' is printed b.test() # 'haha' is printed

更多推荐

函数名在python类中未定义

本文发布于:2023-11-28 06:59:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   类中   未定义   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!