"this"在python中等同于"self"吗? [英] Is 'this' the cpp equivalent of 'self' in python?

查看:492
本文介绍了"this"在python中等同于"self"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python方面经验丰富,现在学习cpp可以加快代码的速度.读取后,this似乎等同于self的cpp.我发现了一个问题,它解释了与cpp用户的不同之处的观点,但我想知道python用户观点的任何差异.

解决方案

主要区别在于,在C ++中,您大多不需要this,因为在定义成员和引用成员之间存在语法上的区别. /p>

对比度

Python:

class Foo:
    def __init__(self):
        self._bar = 42

    def baz(self):
        return self._bar += 1

C ++:

class Foo {
    int bar = 42;
public:
    int baz() { return bar += 1; }
}

I'm experienced in Python and now learning cpp to speed up code. After reading a bit this seems to be the cpp equivalent of self. I found a question explaining the difference from a cpp user's point of view but I'd like to know any differences for a python user's point of view.

解决方案

The major difference is that you mostly don't need this in C++, because there is a syntactic distinction between defining a member and referring to it.

Contrast

Python:

class Foo:
    def __init__(self):
        self._bar = 42

    def baz(self):
        return self._bar += 1

C++:

class Foo {
    int bar = 42;
public:
    int baz() { return bar += 1; }
}

这篇关于"this"在python中等同于"self"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆