自我做什么?
可能的重复:
Python“self”关键字
请原谅我,如果这是一个令人难以置信的菜鸟问题,但我从来没有理解 self Python。它有什么作用?当我看到
def example(self, args):
return self.something
他们在做什么时?我想我也在函数中的某个地方看到了 args 。请简单解释一下:P
Possible Duplicate:
Python 'self' keyword
Forgive me if this is an incredibly noobish question, but I never did understand self in Python. What does it do? And when I see things like
def example(self, args):
return self.something
what do they do? I think I've seen args somewhere in a function too. Please explain in a simple way :P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您好像偶然发现了 Python 的面向对象功能。
self
是对对象的引用。它与许多 C 风格语言中的this
概念非常接近。查看这段代码:It sounds like you've stumbled onto the object oriented features of Python.
self
is a reference to an object. It's very close to the concept ofthis
in many C-style languages. Check out this code:self
是对该方法(在本例中为example
函数)所属类的实例的引用。您需要查看 Python 文档关于类系统,全面介绍 Python 的类系统。您还需要查看这些答案 其他 有关的问题 主题 上 Stackoverflow。
self
is the reference to the instance of the class that the method (theexample
function in this case) is of.You'll want to take a look at the Python docs on the class system for a full introduction to Python's class system. You'll also want to look at these answers to other questions about the subject on Stackoverflow.
它本身是对当前类实例的引用。在您的示例中,
self.something
引用example
类对象的something
属性。Self it a reference to the instance of the current class. In your example,
self.something
references thesomething
property of theexample
class object.