使用 self 获取类中的变量?
这可能是一个愚蠢的问题,但是如果我有一个名为 nstate 的变量,在类中用适当的 getter 和 setter 定义,并且我想从类中获取它的值,那么使用
self.nstate
或
nstate
获取它的值 是否更好同一级别内的价值?有审美上的好处吗?性能上有什么区别吗?
This is probably a silly question, but if i have a variable called, say nstate, defined with appropriate getters and setters within a class, and I would like to get its value from within the class, is it better to use
self.nstate
or
nstate
when getting its value within the same class? Is there an aesthetic benefit? Is there any difference in performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 Self 就像 Java,那么
nstate
比self.nstate
更可取,因为需要读取的符号更少,使得代码可以更快地阅读和理解。一行代码中的符号越多,阅读所需的时间就越长。另一方面,语言,如果语言类似于 python,那么您必须使用 self.nstate,因为
nstate
将始终引用本地范围,即使nstate< /code> 不存在于本地范围内。
If Self is like Java then
nstate
is preferable toself.nstate
this is because there are less symbols to read, making code quicker to read and comprehend. The more symbols in a line of code the longer it takes to read.On the other hand the language, if the language is like python then you MUST use
self.nstate
asnstate
will always refer to the local scope even ifnstate
doesn't exist in the local scope.