为什么一个类只有一个实例? [英] Why a class when there will only be one instance?

查看:71
本文介绍了为什么一个类只有一个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建两个或更多实例时,我看到了一个类的价值,但是当只有一个

实例时,Python程序员经常使用一个类。

这有什么好处?它的缺点在于,无处不在需要大量的自我。

,使代码的可读性降低。另外,由于Python的优势在于快速的应用程序开发,所以它会减慢一个人的价值,因为他们需要投入所有这些自我。代码对我来说似乎更清晰

没有只有一个实例的类。哦,同样,这个类的所有方法

都必须在它们前面添加实例名称。


如果有人能解释一下,我将不胜感激做这个的好处,或至少是它出现的社会学原因。

Mitchell Timin


- -

许多人在追求他们所选择的道路时很顽固,很少有人追求这个目标。 - Friedrich Nietzsche

http://annevolve.sourceforge.net 这就是我现在所做的事情。

人类可以在这个地址给我写信:zenguy at shaw dot ca

解决方案

< blockquote> On Ter 25 Mai 2004 21:43, Se******@SeeBelow.Nut 写道:

我会在创建两个或更多实例时看到类的值,但是当程序员只有一个
实例时,它会经常使用一个类。这有什么好处?它的缺点是在任何地方都需要大量的自我,这使得代码的可读性降低。此外,由于Python的优势在于快速的应用程序开发,因此它会减慢一个人必须放入所有这些自我。代码对我来说似乎更清晰
没有只有一个实例的类。哦,同样,这个类的所有方法都必须在它们之前加上实例名称。

如果有人能解释做的好处,我将不胜感激
这个或者至少是它发生的社会学原因。




你怎么能保证这样的代码永远不会被再次使用,

在哪里?


从一开始就允许代码重用更容易,恕我直言。


-

戈多伊。 < go *** @ ieee.org>


2004-05-26, Se ****** @ SeeBelow.Nut < Se ****** @ SeeBelow.Nut>写道:

我会在创建两个或更多实例时看到类的值,但是当只有一个实例时,Python程序员经常使用一个类。这有什么好处?




当有人想要在

中使用那些需要多个实例的代码时会发生什么?


-

Steve C. Lamb |我是你的牧师,我是你的缩水,我是你的

PGP钥匙:8B6E99C5 |与灵魂交换机的主要连接。

------------------------------- + - -------------------------------------------


2004年5月26日星期三00:43:49 +0000,SeeBelow写道:

当两个或多个实例出现时,我看到一个类的值创建,但是Python程序员经常使用一个类,只有一个
实例。
这有什么好处?它的缺点是在任何地方都需要大量的自我,这使得代码的可读性降低。此外,由于Python的优势在于快速的应用程序开发,因此它会减慢一个人必须放入所有这些自我。代码对我来说似乎更清晰
没有只有一个实例的类。哦,同样,这个类的所有方法都必须在它们之前加上实例名称。

如果有人能解释做的好处,我将不胜感激
这个或至少是它出现的社会学原因。
Mitchell Timin




如果计划制作更多实例,定义一个类可能会有用下线

这条线。这是一个很好的面向对象战略。我明白你不喜欢''自我'。

看起来好像很混乱。在我的代码中,我将它缩短为's'。在ruby中,类

变量以''@'为前缀,这使得它们更容易识别

代码,并且比输入''self'更容易。我希望python有这样的东西

。我还认为必须在每个

函数定义中指定类变量有点傻。 Ruby也摆脱了它。


--SegPhault


I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.''s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.

Mitchell Timin

--
"Many are stubborn in pursuit of the path they have chosen, few in
pursuit of the goal." - Friedrich Nietzsche

http://annevolve.sourceforge.net is what I''m into nowadays.
Humans may write to me at this address: zenguy at shaw dot ca

解决方案

On Ter 25 Mai 2004 21:43, Se******@SeeBelow.Nut wrote:

I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.''s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.



How can you guarantee that such a code will never ever be used again,
anywhere?

It''s easier to allow for code reuse from the beginning, IMHO.

--
Godoy. <go***@ieee.org>


On 2004-05-26, Se******@SeeBelow.Nut <Se******@SeeBelow.Nut> wrote:

I see the value of a class when two or more instances will be created, but
Python programmers regularly use a class when there will only be one
instance. What is the benefit of this?



What happens when down the line someone wants to use that code in
something that requires multiple instances?

--
Steve C. Lamb | I''m your priest, I''m your shrink, I''m your
PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------


On Wed, 26 May 2004 00:43:49 +0000, SeeBelow wrote:

I see the value of a class when two or more instances will be created,
but Python programmers regularly use a class when there will only be one
instance.
What is the benefit of this? It has a disadvantage of a whole lot of
"self."
being required everywhere, making the code less readable. Also, since a
strength of Python is rapid application development, it slows one down
to have to put in all those self.''s. The code seems much cleaner to me
without classes that have only one instance. Oh, also, all the methods
of this class will have to have the instance name prepended to them.

I would appreciate it if someone could explain the advantages of doing
this, or at least the sociological reasons why it occurs.

Mitchell Timin



defining a class may be useful if you plan on making more instances down
the line. It''s a good OO strategy. I do understand your dislike of ''self''.
It does seem like clutter. In my code, I shorten it to ''s''. In ruby, class
variables are prefixed with an ''@'', which makes them easier to discern in
code, and it is easier than typing ''self''. I wish python had something
like that. I also think that having to specify the class variable in every
function definition is a bit silly. Ruby gets rid of that too.

--SegPhault


这篇关于为什么一个类只有一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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