Python中的OOP技术 [英] OOP techniques in Python

查看:81
本文介绍了Python中的OOP技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想是否有必要像在其他语言中那样在Python中应用某些特定的OOP

技术。


即我们通常定义私人财产并提供公共职能

来访问它们,形式为:

get {...} set {...}


我们应该在Python中做同样的事情:


self .__ privateAttr =''some val''


def getPrivateAttr(self):

返回self .__ privateAttr


或者这样做没有意义吗?


我想到了其他一些技巧,但我认为Python倾向于允许程序员访问他想要的东西,即使他不应该b $ b或b以字典或列表的形式,而不是这样做的方法。

I''ve been thinking if there''s a point in applying some specific OOP
techniques in Python as we do in other languages.

i.e. we usually define private properties and provide public functions
to access them, in the form of:
get { ... } set { ... }

Should we do the same in Python:

self.__privateAttr = ''some val''

def getPrivateAttr(self):
return self.__privateAttr

Or there''s no point in doing so?

Some other techniques come to mind, but I think that Python tends to
allow the programmer to access stuff he wants even though he shouldn''t
or in the form of a dict or list, rather than a method to do so.

推荐答案

Panos Laganakos写道:
Panos Laganakos wrote:
即我们通常定义私有属性并提供公共功能ons
访问它们,形式如下:
get {...} set {...}

我们是否应该在Python中做同样的事情:

self .__ privateAttr =''some val''

def getPrivateAttr(self):
返回self .__ privateAttr

或者没有这样做了吗?
i.e. we usually define private properties and provide public functions
to access them, in the form of:
get { ... } set { ... }

Should we do the same in Python:

self.__privateAttr = ''some val''

def getPrivateAttr(self):
return self.__privateAttr

Or there''s no point in doing so?



如果你想做的不仅仅是设置一个属性或者返回它,那么

定义一个属性,但是如果你只是想要保存价值没有意义

使用房产。在其他一些语言中,您无法将属性

更改为属性而无需更改类的接口,因此在这些

语言中,您需要将所有内容都设置为属性以防万一你需要

它将来成为一个属性。


在Python中,当你需要它成为一个属性时你可以不用它来改变它/>
打破任何东西,所以没有必要过早地混淆它。


If you want to do more than just setting an attribute or returning it then
define a property, but if you just want to save the value there is no point
using a property. In some other languages you cannot change an attribute
into a property without changing the interface to the class, so in those
languages you need to make everything a property just in case you ever need
it to be a property in the future.

In Python, when you need it to become a property you can change it without
breaking anything, so there is no need to obfuscate it prematurely.


Panos Laganakos写道:
Panos Laganakos wrote:
我们通常定义私有属性和提供公共功能以访问它们的形式:
get {...} set {...}

我们应该在Python中做同样的事情:

self .__ privateAttr =''some val''

def getPrivateAttr(self):
返回self .__ privateAttr

或者那里这样做没有意义吗?
we usually define private properties and provide public functions
to access them, in the form of:
get { ... } set { ... }

Should we do the same in Python:

self.__privateAttr = ''some val''

def getPrivateAttr(self):
return self.__privateAttr

Or there''s no point in doing so?




这样做没有意义。每当

可能时你就应该使用普通属性(也就是说,无论何时你真的只是做一个get或set,

没有计算)。使用getter和setter的唯一原因是,如果需要,可以稍后更改实现。但是python

允许你用属性来做这个:



There is no point in doing so. You should use plain attributes whenever
possible (that is, whenever you''re really just doing a get or a set,
with no computation). The only reason to use getters and setters is so
that you can change the implementation later if you need to. But python
allows you to do this with properties:

C类(对象) ):
.... def __init __(self,x):

.... self.x = x

.... C(42 ).x
42级C(对象):
.... def _get_x(个体经营):

....返回self._x * 2

.... def _set_x(self,value):

.... self._x = value / 2

.... x = property (_get_x,_set_x)

.... def __init __(self,x):

.... self.x = x

.... C(42).x
class C(object): .... def __init__(self, x):
.... self.x = x
.... C(42).x 42 class C(object): .... def _get_x(self):
.... return self._x * 2
.... def _set_x(self, value):
.... self._x = value / 2
.... x = property(_get_x, _set_x)
.... def __init__(self, x):
.... self.x = x
.... C(42).x






哪个不应该被解释为说你现在应该开始写一堆

的房产了。 ;)相反,只有当你发现某些东西必须保留一个属性时才会引入一个属性(可能是为了倒退

兼容性原因)但是无论出于何种原因它现在需要一些

额外的计算。


STeVe


42

Which should not be interpreted as saying you should start writing a
bunch of properties now. ;) Instead, only introduce a property when you
find that something must remain an attribute (presumably for backwards
compatibility reasons) but for whatever reason it now needs some
additional computation.

STeVe


为什么?对我来说,当我看到自我时,这是有道理的.__ m_var,我正在处理

与成员变量taht派生类将无法查看/访问。


Philippe


Steven Bethard写道:
Why is that ? to me it makes sense when I see self.__m_var that I''m dealing
with a member variable taht derived classes will not see/access.

Philippe


Steven Bethard wrote:
Panos Laganakos写道:
Panos Laganakos wrote:
我们通常定义私有财产并提供公共功能以访问它们,形式如下:
get {...} set {...}

我们是否应该在Python中执行相同操作:

self .__ privateAttr =''some val''

def getPrivateAttr(self):
返回self .__ privateAttr

或者那里'这样做没有意义吗?
we usually define private properties and provide public functions
to access them, in the form of:
get { ... } set { ... }

Should we do the same in Python:

self.__privateAttr = ''some val''

def getPrivateAttr(self):
return self.__privateAttr

Or there''s no point in doing so?



没有必要这样做。你应该在可能的时候使用普通属性(也就是说,无论什么时候你真的只是做一个get或set,
没有计算)。使用getter和setter的唯一原因是,如果需要,可以稍后更改实现。但是python
允许你使用属性来执行此操作:



There is no point in doing so. You should use plain attributes whenever
possible (that is, whenever you''re really just doing a get or a set,
with no computation). The only reason to use getters and setters is so
that you can change the implementation later if you need to. But python
allows you to do this with properties:

>>> class C(object):... def __init __(self,x):
... self.x = x
...>>> C(42).x 42>>> class C(object):... def _get_x(self):
...返回self._x * 2
... def _set_x(self,value):
... self._x = value / 2
... x = property(_get_x,_set_x)
... def __init __(self,x):
... self.x = x
...>>> C(42).x
>>> class C(object): ... def __init__(self, x):
... self.x = x
... >>> C(42).x 42 >>> class C(object): ... def _get_x(self):
... return self._x * 2
... def _set_x(self, value):
... self._x = value / 2
... x = property(_get_x, _set_x)
... def __init__(self, x):
... self.x = x
... >>> C(42).x


42

这不应该被解释为你现在应该开始写一堆
属性。 ;)相反,只有在您发现某些东西必须保留属性时才会引入属性(可能是出于向后兼容性原因),但无论出于何种原因,它现在需要一些额外的计算。

STeVe


42

Which should not be interpreted as saying you should start writing a
bunch of properties now. ;) Instead, only introduce a property when you
find that something must remain an attribute (presumably for backwards
compatibility reasons) but for whatever reason it now needs some
additional computation.

STeVe






这篇关于Python中的OOP技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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