为什么不太重视私人数据? [英] Why less emphasis on private data?

查看:55
本文介绍了为什么不太重视私人数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自C ++ / C#背景,缺乏对私人数据的重视

对我来说似乎很奇怪。我经常发现包装私有数据对

有用,可以防止错误并强制执行错误检查..


在我看来(可能是错误的)Python更喜欢离开班级

数据公开。这个选择背后的逻辑是什么?


感谢任何见解。

Coming from a C++ / C# background, the lack of emphasis on private data
seems weird to me. I''ve often found wrapping private data useful to
prevent bugs and enforce error checking..

It appears to me (perhaps wrongly) that Python prefers to leave class
data public. What is the logic behind that choice?

Thanks any insight.

推荐答案

< a href =mailto:ti ******** @ gmail.com> ti ******** @ gmail.com schrieb:
ti********@gmail.com schrieb:

来自C ++ / C#背景,缺乏对私人数据的重视

对我来说似乎很奇怪。我经常发现包装私有数据对

有用,可以防止错误并强制执行错误检查..

我觉得(也许是错误的)Python更喜欢离开类

数据公开。这个选择背后的逻辑是什么?


感谢任何见解。
Coming from a C++ / C# background, the lack of emphasis on private data
seems weird to me. I''ve often found wrapping private data useful to
prevent bugs and enforce error checking..
It appears to me (perhaps wrongly) that Python prefers to leave class
data public. What is the logic behind that choice?

Thanks any insight.



Python不喜欢课堂上的公共数据。它让程序员选择了
。您可以使用''__''前缀定义您自己的私有实例变量(或

函数):


示例:

class Foo:

def __init __(自我,数据):

self .__ data = data


def get_data(self ):

返回self .__数据

Python doesn''t prefer public data in classes. It leaves the choice to
the programmer. You can define your own private instance variables (or
functions) by using a ''__'' prefix:

example:
class Foo:
def __init__(self, data):
self.__data = data

def get_data(self):
return self.__data


>> f = Foo(''bar'')
f .__ data
>>f = Foo(''bar'')
f.__data



Traceback(最近一次通话) last):

文件"< stdin>",第1行,< module>
AttributeError:Foo实例没有属性''__data''

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute ''__data''


>> f.get_data()
>>f.get_data()



''bar''

''bar''


TI ****** **@gmail.com schrieb:
ti********@gmail.com schrieb:

来自C ++ / C#背景,缺乏对私人数据的重视

对我来说似乎很奇怪。我经常发现包装私有数据对

有用,可以防止错误并强制执行错误检查..


在我看来(可能是错误的)Python更喜欢离开班级

数据公开。这个选择背后的逻辑是什么?
Coming from a C++ / C# background, the lack of emphasis on private data
seems weird to me. I''ve often found wrapping private data useful to
prevent bugs and enforce error checking..

It appears to me (perhaps wrongly) that Python prefers to leave class
data public. What is the logic behind that choice?



对于Java

和C ++,私有数据是一种约定,而不是严格的强制执行。


根据您的C ++编译器,一个简单的


#define私人公共

将允许您访问所需的所有数据。除了向void *指针投射

并且只是访问私有部分这一事实不是火箭

科学。


这同样适用于java,无论出于何种原因(我认为是
序列化),你可以通过反射访问私有字段。


在python中,私有成员是通常使用单个或双重

下划线声明。而基本的想法是:如果你篡改这个,你就会被警告。在同意成年人之间应该采用哪种方式进行编码。


说实话:我已经偶然发现了更多不必要的箍圈来跳过

到期私人声明而不是我利用的错误

我被编译器告知的事情是不要篡改它。


摘要:不重要,忘了它,享受python!


Diez

Private data is a convention, not a strict enforcement, for both Java
and C++.

Depending on your C++ compiler, a simple

#define private public

will give you access to all data you want. Besides the fact that casting
to a void* pointer and just accessing the private parts isn''t rocket
science.

The same applies to java, for whatever reasons (I presume
serialization), you can access private fields via reflection.

In python, private members are usually declared using a single or double
underscore. And the basic idea is: "if you tamper with this, you''ve been
warned". Which is the way coding between consenting adults should be.

To be honest: I''ve stumbled over more cases of unescessary hoops to jump
through due to private declarations than bugs caused of me exploiting
things I''ve been told by the compiler not to tamper with it.

Summary: not important, forget about it, enjoy python!

Diez




timeComing from C ++ / C#background ,没有强调私人

timedata对我来说似乎很奇怪。


Python并没有试图保护你免受你使用的代码的作者。你b $ b应该足够聪明,明智地使用它。另一方面,缺乏真正私有数据和方法的原始作者意味着一段

代码的原始作者并不需要预测未来的所有用途代码将是

put。以下是我们都是成年人的几个项目。

http://spyced.blogspot.com/2005/06/a...ok-python.html
http://www.mail- archive.com/tu***@py.../msg17806.html


Skip

timeComing from a C++ / C# background, the lack of emphasis on private
timedata seems weird to me.

Python doesn''t try to protect you from the authors of the code you use. You
should be intelligent enough to use it wisely. On the flip side, the lack
of truly private data and methods means the original author of a piece of
code doesn''t need to anticipate all future uses to which the code will be
put. Here are a couple items along the lines of "we''re all adults here".

http://spyced.blogspot.com/2005/06/a...ok-python.html
http://www.mail-archive.com/tu***@py.../msg17806.html

Skip


这篇关于为什么不太重视私人数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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