sil*_*llz -5 python pycharm python-3.x
我来自 Java 和 C# 背景并学习 Python。我想知道为什么这段代码在 pycharm 中给我一个错误说
Unresolved Reference 'methodA'
Run Code Online (Sandbox Code Playgroud)
用这个代码
def a():
print("hi")
def b():
a()
Run Code Online (Sandbox Code Playgroud)
这段代码(您发布的整个代码)应该可以完美运行,因为该名称a
已被引入全局范围:
def a():
print("hi")
def b():
a()
b() # call b, which in turn calls a
Run Code Online (Sandbox Code Playgroud)
但是这段代码(您可能引用的内容)应该会失败,因为该名称a
是C
. 成员名称不在其他成员函数的范围内:
class C:
def a():
print("hi")
def b():
a()
o = C()
o.b() # should fail for several reasons.
Run Code Online (Sandbox Code Playgroud)
另请注意,与 C++ 或 Java 的隐式this
指针不同,self
必须始终显式使用引用。试试这个:
class C:
def a(self):
print("hi")
def b(self):
self.a()
o = C()
o.b() # calls C.b(o), which in turn calls C.a(o)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5349 次 |
最近记录: |