简体   繁体   English

在python中,为什么使用函数获取类成员?

[英]In python,why use function to get class member?

Hi, recently I read some codes of others , and found the differences between others and mine is that they often write a function to get class member. 嗨,最近,我阅读了其他代码,发现其他代码与我的代码之间的区别在于它们经常编写函数来获取类成员。 Like 喜欢

def GetHeight
    return self.height

instead of just using 而不是仅仅使用

self.height

in the program. 在程序中。

I mean, in C++, I know the function of this is to get private member. 我的意思是,在C ++中,我知道它的功能是获取私有成员。
In python, though everything is set as public (in normal cases) , 在python中,尽管一切都设置为public (在正常情况下),
Why it has reason to be used in python? 为什么有理由在python中使用它? Is that just bad code? 那只是不好的代码吗? Or something else I ignore? 还是我忽略的其他事情?
I am kinda new to python, so I will appreciate it if you give some advice. 我是python的新手,所以如果您提供一些建议,我将不胜感激。

EDIT: 编辑:
Similar Question It seems my question has been identified as the duplicate of another one. 相似的问题看来我的问题已被确认为另一个问题的重复。 I checked the answer and found the answer of that is more like general advice instead of pythonic programming. 我检查了答案,发现答案更像是一般性建议,而不是pythonic编程。

While the answer below introduces a feature of python -- property 虽然以下答案介绍了python的功能- 属性
That possibly cannot be learned from that question, so it's the reason I think my question is unique. 这可能无法从该问题中学到,所以这就是我认为我的问题独特的原因。

In Python it is unnecessary to create a getter method (especially if the getter simply returns the attribute value with no checks or modifications). 在Python中,没有必要创建一个getter方法(尤其是如果getter仅返回没有检查或修改的属性值)。 You can implement meaningful get and set methods but they should actually do something. 您可以实现有意义的get和set方法,但是它们实际上应该做一些事情。 The preferred approach is to use property decorators as described here: How does the @property decorator work? 首选方法是按如下所述使用属性装饰器: @property装饰器如何工作?

class Building(object):
    def __init__(self):
        self._height = 100

    @property
    def height(self):
        return self._height

    @height.setter
    def height(self, value):
        if 0 < value < 1000:
            self._height = value
        else:
            raise ValueError('Attribute: height must be between 0 and 1000.')

Then the property methods are used when reading from or assigning to the attribute: 然后从属性读取或分配属性时使用属性方法:

b = Building()

print(b.height)
100

b.height = 90
print(b.height)
90

b.height = 2000
print(b.height)
90

By convention, reserved attributes are preceded by a single underscore and private attributes by a double underscore. 按照惯例,保留属性前面带有单个下划线,私有属性前面带有双下划线。 However, this is more to alert users of the class that the attributes should be handled with care and nothing prevents them from accessing the attributes. 但是,这更多是为了提醒该类的用户应谨慎处理属性,并且没有什么可以阻止他们访问属性。

it's a matter of preference. 这是一个偏好问题。 python lets the programmer be OO or imperative as suits. python让程序员适合使用OO或命令式。 the whole notion of accessors and mutators is old fashioned for some and beloved by others. 存取器和mutator的整个概念对于某些人而言是过时的,而另一些人则钟爱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM