班级朋友 [英] Class Friends

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

问题描述



您好!


是否可以在Python中成为朋友?或者将

坚持到Java风格是否更好? .isThis,.getThat,.set这个样式函数?


我知道类通常按逻辑分组为单独的文件

无论如何。所以,这并不总是必要的。但我是完美主义者,并且想知道在Python中是否可以建立友谊。


谢谢!

se ******* @ yahoo.com

推荐答案

2004年8月27日星期五15:19:53 +0000,Sean Don写道:
On Fri, 27 Aug 2004 15:19:53 +0000, Sean Don wrote:

你好!

是有可能在Python中成为朋友吗?或者更好地坚持使用Java风格。 .isThis,.getThat,.set这个样式函数?

我知道类通常按逻辑分组为单独的文件
。所以,这并不总是必要的。但我是完美主义者,并且想知道在Python中是否可以建立友谊。

Hello!

Is it possible to make classes friends in Python? Or is it better to just stick
to "Java style" .isThis, .getThat, .setThis style functions?

I understand that classes are generally grouped logically into separate files
anyway. So, this isn''t always necessary. But I''m a perfectionist and was
wondering if friendships were possible in Python.




在Python中,没有太多的想法;私人英寸; Python的哲学

是我们都在这里同意成年人。因此,对于朋友来说,没有太多想法。无论是。在Java术语中,在技术层面上每个人

已经是其他人的朋友了。


最终,它是友谊的人类意义。更重要的是(b)与其他班级内部结合的类别,而Python

允许你按照自己的意愿处理。

在我多年的Python编程中,只有一次我生成了一个

" friend"在人类意义上的阶级。一般来说有很多方法。


作为最后一点,不要使用getThat或setThis;这是一个黑客攻击

Java的弱点。查找属性在Python手册中使用它;

它既适用于重新分解类,也适用于新类。这是一个原因,即Python侥幸逃脱的原因。它的工作方式;除非你以自己的方式打破课程的封装,否则班级作者总是可以通过财产获取属性访问等内容。



class MyXActsStrange(object):

def __init __(self):

self._x = 0

def getX(self):

返回self._x + 1

def setX(self,val):

self._x = val

x = property(getX,setX)



In Python, there isn''t much of an idea of "Private"; Python''s philosophy
is "We''re all consenting adults here." Thus, there isn''t much of an idea
of "friends" either. In Java terminology, on a technical level everybody
is already friends with everybody else already.

Ultimately, it is the human meaning of "friendship" that is more
important (classes that twiddle with other classes internals), and Python
allows you to deal with that as you see fit.

In all my years of Python programming, only once have I generated a
"friend" class in the human sense. Generally there are ways around it.

As a final note, don''t use ''getThat'' or ''setThis''; that''s a hack around
Java''s weaknesses. Look up "property" in the Python manual and use that;
it is good both for re-factoring classes and for new ones too. This is one
reason that Python "gets away with" the way it works; unless you go out of
your way to break a class''s encapsulation, the class author can always
catch things like attribute access with a property.

class MyXActsStrange(object):
def __init__(self):
self._x = 0
def getX(self):
return self._x + 1
def setX(self, val):
self._x = val
x = property(getX, setX)

a = MyXActsStrange()
ax
1 ax = 4
ax
a = MyXActsStrange()
a.x 1 a.x = 4
a.x



5

非常有用。


(有些人会使用self .__ x,来获取伪造的私有名称

。如果安全不是'''我的电话就是因为语言已经取决于客户端程序员的行为了。我认为Java可以获得良好的安全性,但是完美的并不是很多。 C ++已经有效地像Python那样宽松,它只会使箍更难。)


5
Very useful.

(Some people would use "self.__x", to get the psuedo-private name munging
invoked. My call is if the "security" isn''t perfect there isn''t much
point, as the language is already depending on the client programmer to
behave. I think Java can get good security but C++ is already effectively
as permissive as Python, it just makes the hoops harder.)


2004年8月27日星期五16:55: 28 GMT,Jeremy Bowers< je ** @ jerf.org>写道:
On Fri, 27 Aug 2004 16:55:28 GMT, Jeremy Bowers <je**@jerf.org> wrote:
(有些人会使用self .__ x,来调用伪造的私人名称。我的电话是安全并不完美没有太多意义,因为语言已经取决于客户端程序员的行为。我认为Java可以获得良好的安全性,但C ++已经像Python一样有效了,它只会使箍更难。)
(Some people would use "self.__x", to get the psuedo-private name munging
invoked. My call is if the "security" isn''t perfect there isn''t much
point, as the language is already depending on the client programmer to
behave. I think Java can get good security but C++ is already effectively
as permissive as Python, it just makes the hoops harder.)




实际上,双重修正更多是关于停止子类

来踩一些内部基类的细节。一般来说,

虽然,我发现这是一个完全痛苦的屁股 - 已经过多次我已经过了很多次使用外部库并希望将
挂钩到基类来修复某些东西,并且必须使用损坏的

名称。


我已经考虑过一个圣战,尝试使用从标准库中删除的双重下限
。也许一旦2.4出局,我会

接受这个。



Really, the double-under mangling is more about stopping a subclass
from stomping on some internal detail of the base class. In general,
though, I''ve found it to be a complete pain in the arse - there''s been
too many times where I''ve been using an external library and wanted to
hook into the base class to fix something, and had to use the mangled
name.

I''ve considered a jihad to try and get uses of the double-under
mangling removed from the standard library. Maybe once 2.4 is out I''ll
take this on.


Sean Don< a@c.d>写道:
Sean Don <a@c.d> wrote:
你好!

是否有可能在Python中成为朋友?或者只是
坚持Java风格更好? .isThis,.getThat,.set这个样式的功能?


两者都没有。 "朋友"在Python术语中没有可能的含义。访问者

方法在Python中是一个可怕的想法:如果你需要访问

属性来触发某些代码的执行,为什么,使用''property'' - -

这就是它的用途!


我知道类通常按逻辑分组为单独的
文件。所以,这并不总是必要的。但我是完美主义者,并且想知道在Python中是否可以建立友谊。
Hello!

Is it possible to make classes friends in Python? Or is it better to just stick to "Java style" .isThis, .getThat, .setThis style functions?
Neither. "friends" has no possible meaning in Python terms. Accessor
methods are a horrid idea in Python: if you ever need access to an
attribute to trigger the execution of some code, why, use ''property'' --
that''s what it''s THERE for!

I understand that classes are generally grouped logically into separate files anyway. So, this isn''t always necessary. But I''m a perfectionist and was
wondering if friendships were possible in Python.




在Python环境中,友谊可能有什么意义? ......?!

Alex



What could ''friendship'' possibly ever MEAN in a Python context...?!
Alex


这篇关于班级朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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