我应该使用“自我"吗?定义实例/类的变量/对象,我将不需要从外部访问? [英] Should I use "self" to define variables / objects of class instanced which I will not need to reach from the outside?

查看:97
本文介绍了我应该使用“自我"吗?定义实例/类的变量/对象,我将不需要从外部访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个完整的初学者,但是对 Python 来说是个新手.今天在从事一个项目时,我只是有一个主意,想知道"自我"的用法;关于我过去已经阅读了一段时间的书,但我仍然无法弄清它是否总是必要的.我的问题仅涉及类的实例和实例参数/变量. 这个问题不是关于会影响所有实例的类变量.

I am not a complete beginner but fairly new to Python. Whilst working on a project today I just had an idea and wondered regarding the usage of "self"; about which I've been reading for the past some while and I still can not figure out if it's always necessary or not. My question solely concerns instances of classes and instance parameters/variables. This question is not about Class Variables which affect all instances.

示例:

class C:
    def __init__(self, parent = None):
        super(C, self).__init__(parent)
        self.some_temp_var = AnotherClass()
        self.important_property = self.some_temp_var.bring_the_jewels()

ins_c = C()
print ins_c.important_property 

在上面的代码中,我在两个变量声明( some_temp_var important_property )之前都使用了 self .

In the above code, I use self before both variable declarations (some_temp_var and important_property).

稍后需要从外部(创建实例的地方)访问 ** important_property **,甚至可能对其进行修改.

I will need to access **important_property** later from the outside (where instance's been created) and maybe even modify it.

但是我不需要访问AnotherClass()实例和/或指向该实例的变量( some_temp_var ).我只需要一次它类的实例,并且只需要执行一次它的方法Bring_the_jewels即可填充 important_property 的值.

But I will not need access to the instance of AnotherClass() and/or variable pointing to it (some_temp_var). I just need an instance of it class once, and I need to execute its method bring_the_jewels only once to populate value of important_property.

在声明该变量之前,我还应该使用self吗?

Should I still use self before declaring that variable as well?

self.some_temp_var = ....
self.important_property = ....

或者可以是:

some_temp_var = ....
self.important_property = ....

谢谢您的帮助.

Ps.我做了很长的研究.由于缺乏英语和/或CS知识,我可能找不到当前存在的副本,但是我确实进行了搜索并且做了很多搜索.在将此问题称为重复的"或非建设性的"之前,请通读它.这是一个有明确答案的问题,非常重要且复杂.谢谢您的理解.

推荐答案

如果不使用self.some_temp_var,则您正在定义一个局部变量,该局部变量将在构造函数之后进行垃圾回收.

If you dont use self.some_temp_var, then you are defining a local variable which will be garbage collected after the constructor.

因此,是的,如果要定义一个实例属性,该实例属性应与实例保持一致,则需要self.您是否需要将其公开或私有是命名的问题.在受保护的前面加上_或在私有的前面加上__作为前缀.

So yes you need self if you want to define an instance attribute that should persist with the instance. Whether you need it to be public or private is a matter of naming. Prefix it with an _ for protected or __ for private.

如果some_temp_var只是构造函数的临时文件,而您再也不需要它了,那么不需要,则不需要self.它与持久性和成员属性有关.纯粹出于在单个方法中使用变量的事实,您不需要self.

If some_temp_var is just a temp for the constructor and you never need it again then no, you dont need self. It is about persistence and member attributes. You do not need self for the pure fact of working with a variable within a single method.

考虑一下...如果some_temp_var是您为了达到实际结果值而建立的一些巨大数据结构,该怎么办?您不想使用self存储临时变量,因为它不会释放内存,直到您删除它或实例为止.您希望在不再需要它时对其进行清理.

Consider this... What if some_temp_var were some huge data structure that you build up to arrive at the real result value? You dont want to use self to store a temp variable because it wont free memory until you delete it or the instance. You want it to be cleaned up when it is no longer needed.

这篇关于我应该使用“自我"吗?定义实例/类的变量/对象,我将不需要从外部访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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