了解Python中的数据封装 [英] Understanding data encapsulation in Python
问题描述
我正在阅读我们如何确保数据封装在python中.其中一个博客说数据封装意味着,我们只能通过getter和setter访问私有属性"
I am reading up on how we ensure data encapsulation in python.One of the blog says "Data Encapsulation means, that we should only be able to access private attributes via getters and setters"
请考虑以下博客的摘要:
Consider the following snippets from the blog:
class Robot:
def __init__(self, name=None, build_year=None):
self.name = name
self.build_year = build_year
现在,如果我按如下所示创建该类的对象:
Now, if i create the object of the class as below:
obj1=Robot()
obj1.name('Robo1")
obj1.build_year("1978")
当前,我可以直接访问已定义为public的属性(无需 __
标记)
Currently, i can access the attributes directly as i have defined them public(without the __
notation)
现在要确保数据封装,我需要将属性定义为私有使用 __
表示法并通过getter和setter访问私有属性.
Now to ensure data encapsulation, i need to define the attributes as privates
using the __
notation and access private attributes via getters and setters.
因此,新的类定义如下:
So the new class definition is as follows:
class Robot:
def __init__(self, name=None, build_year=2000):
self.__name = name
self.__build_year = build_year
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_build_year(self, by):
self.__build_year = by
def get_build_year(self):
return self.__build_year
现在,我将实例实例化如下:
Now i instantiate the class as below:
x = Robot("Marvin", 1979)
x.set_build_year(1993)
这样,我就实现了数据封装,因为不再可以直接访问私有数据成员,而只能通过类方法进行访问.
This way, i achive data encapsulation as private data members are no longer accessed directly and they can only be accessed via the class methods.
Q1:我们为什么要这样做?我们在保护谁免受代码侵害?谁在外面?拥有源代码的任何人都可以根据自己的需求进行调整,那么为什么我们根本要添加额外的方法(获取/设置)来修改/调整属性?
Q1:Why are we doing this? Who are we protecting the code from? Who is outside world?Anyone who has the source code can tweak it as per their requirement, so why at all do we add extra methods(get/set) to modify/tweak the attributes?
Q2:以上示例是否被视为数据封装
?
Q2:Is the above example considered data encapsulation
?
推荐答案
关于源代码:有时您向其他人提供编译的python文件,这些文件不显示源代码,并且您不希望别人弄乱直接分配属性.
About source code: sometimes you supply others with compiled python files that does not present the source, and you don't want people to get in mess with direct attribute assignments.
现在,考虑将数据封装作为安全防护措施,这是分配或提供值之前的最后一点:
Now, consider data encapsulation as safe guards, last point before assigning or supplying values:
您可能想使用 set
s验证或处理分配,以确保分配对您的需求有效,或以正确的格式输入变量(例如,您要检查属性 __ build_year
大于1800,或者名称为 string
).在动态语言(例如python)中非常重要,其中的变量未使用特定类型声明.
You may want to validate or process assignments using the set
s, to make sure the assignment is valid for your needs or enters to the variable in the right format, (e.g. you want to check that attribute __build_year
is higher than 1800, or that the name is a string
). Very important in dynamic languages like python where a variable is not declared with a specific type.
同样适用于 get
.您可能希望以小数形式返回年份,但在类中将其用作整数.
Same goes for get
s. You might want to return the year as a decimal, but use it as an integer in the class.
是的,您的示例是基本的数据封装.
Yes, your example is a basic data encapsulation.
这篇关于了解Python中的数据封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!