你怎么称呼一个不打算实例化的类 [英] What do you call a class not intended to be instantiated

查看:43
本文介绍了你怎么称呼一个不打算实例化的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不打算实例化的类。而不是使用

类来创建实例然后对其进行操作,我直接使用类

,使用classmethods。从本质上讲,这个类被用作一个函数

,它可以保持状态从一个调用到下一个调用。


问题是我不知道是什么打电话给这样的事! Abstract

class是不对的,因为这意味着你应该继承

类,然后实例化子类。


你叫什么类这样的? br />

-

史蒂文

I have a class which is not intended to be instantiated. Instead of using
the class to creating an instance and then operate on it, I use the class
directly, with classmethods. Essentially, the class is used as a function
that keeps state from one call to the next.

The problem is that I don''t know what to call such a thing! "Abstract
class" isn''t right, because that implies that you should subclass the
class and then instantiate the subclasses.

What do you call such a class?

--
Steven

推荐答案




这个名为State的正常类

是否足以在调用之间存储状态?

并且......创建一个状态实例?


例如:


class State(object):

""" State()-new state对象


创建一个适合持有应用程序不同状态的新状态对象



在状态机中有用。


这种方式非常简单。您创建一个新的

状态对象,并简单地设置状态。如果状态

不存在,它会添加到它的内部数据

结构中。这样做的原因是比较状态是一致的,并且你不能仅仅与不存在的状态比较


"""


def __init __(self):

" initializes x;请参阅x .__ class __.__ doc__签名"


self._states = {}

self._next = 0


#默认状态


self._add(" START")

self._add(" DONE")

>
def __repr __(自我):

尝试:

return"< State:%s>" %self._state

除了AttributeError:

return"< State:???>"


def __str __(个体经营):

返回self._state


def __eq __(self,s):

返回s in self。 _states和self._state == s

def __lt __(self,s):

在self._states和self._state == s中返回s \

self._states [s]< self._states [self._state]


def __gr __(self,s):

在self._states和self._state == s中返回s \

self._states [s] self._states [self._state]


def _add(self,s):

self._states [s] = self._next

self._next = self._next + 1


def set(self,s):

""" S.set(s)-None


将当前状态设置为s给出的指定状态,

如果它不存在则添加它。

"""


如果s不在self._states:

self._add(s)


self._state = s


欢呼

詹姆斯


2008年9月22日星期一上午8:39,Steven D''Aprano

< st *** @ remove-this-cybersource .com.auwrote:
Hi,

Wouldn''t a normal class called State
suffice for storing state between calls ?
And ... Creating a state instance ?

For example:

class State(object):
"""State() -new state object

Creates a new state object that is suitable
for holding different states of an application.
Usefull in state-machines.

The way this works is rather simple. You create a new
state object, and simply set the state. If the state
doesn''t exist, it''s added to it''s internal data
structure. The reason this is done is so that
comparing states is consistent, and you can''t just
compare with a non-existent state.
"""

def __init__(self):
"initializes x; see x.__class__.__doc__ for signature"

self._states = {}
self._next = 0

# Default States

self._add("START")
self._add("DONE")

def __repr__(self):
try:
return "<State: %s>" % self._state
except AttributeError:
return "<State: ???>"

def __str__(self):
return self._state

def __eq__(self, s):
return s in self._states and self._state == s

def __lt__(self, s):
return s in self._states and self._state == s and \
self._states[s] < self._states[self._state]

def __gr__(self, s):
return s in self._states and self._state == s and \
self._states[s] self._states[self._state]

def _add(self, s):
self._states[s] = self._next
self._next = self._next + 1

def set(self, s):
"""S.set(s) -None

Set the current state to the specified state given by s,
adding it if it doesn''t exist.
"""

if s not in self._states:
self._add(s)

self._state = s

cheers
James

On Mon, Sep 22, 2008 at 8:39 AM, Steven D''Aprano
<st***@remove-this-cybersource.com.auwrote:

我有一个不打算实例化的类。而不是使用

类来创建实例然后对其进行操作,我直接使用类

,使用classmethods。从本质上讲,这个类被用作一个函数

,它可以保持状态从一个调用到下一个调用。


问题是我不知道是什么打电话给这样的事! Abstract

class是不对的,因为这意味着你应该继承

类,然后实例化子类。


你叫什么类这样的? br />

-

史蒂文

-
http://mail.python.org/mailman/listinfo/python-list




-

-

- 问题通过方法解决



--
--
-- "Problems are solved by method"


修复热门帖子。


2008年9月22日星期一08:54:43 +1000,James Mills写道:
Fixing top-posting.

On Mon, 22 Sep 2008 08:54:43 +1000, James Mills wrote:

2008年9月22日星期一上午8:39,Steven D''Aprano

< st *** @ remove-this-cybersource.com.auwrote:
On Mon, Sep 22, 2008 at 8:39 AM, Steven D''Aprano
<st***@remove-this-cybersource.com.auwrote:

>我有一个不打算实例化的类。我没有使用类创建实例然后对其进行操作,而是使用类方法直接使用类。从本质上讲,该类被用作一个函数,可以保持状态从一个调用到下一个调用。
>I have a class which is not intended to be instantiated. Instead of
using the class to creating an instance and then operate on it, I use
the class directly, with classmethods. Essentially, the class is used
as a function that keeps state from one call to the next.



[...]

[...]




不会叫普通班级状态

足以在呼叫之间存储状态?并且...创建一个状态

实例?


例如:
Hi,

Wouldn''t a normal class called State
suffice for storing state between calls ? And ... Creating a state
instance ?

For example:



[snip] ]


对于一个相当小的问题,这是一个相当大的例子。


是的,正常的课程在很多情况下会起作用。在这种情况下,类

本身是由工厂函数生成的,它的输出是

迭代器。不得不打电话:


cls = factory()

instance = cls()

result = instance()

完成任何事情看起来都是过分的,即使你把它写成单行

result = factory()()()。


我没有坚持这个想法,有其他选择(也许工厂

应该实例化该类并返回该类?)但我认为其他人有

使用了这个设计并且有一个名字。

-

史蒂文

[snip]

That''s a rather big example for a rather small question.

Yes, a normal class would work in many cases. In this case, the class
itself is being produced by a factory function, and it''s output is an
iterator. Having to call:

cls = factory()
instance = cls()
result = instance()

to get anything done seems excessive, even if you write it as a one-liner
result = factory()()().

I''m not wedded to the idea, there are alternatives (perhaps the factory
should instantiate the class and return that?) but I assume others have
used this design and have a name for it.
--
Steven


在星期一, 2008年9月22日上午9:05,Steven D''Aprano

< st *** @ remove-this-cybersource.com.auwrote:
On Mon, Sep 22, 2008 at 9:05 AM, Steven D''Aprano
<st***@remove-this-cybersource.com.auwrote:

我不喜欢这个想法,有其他选择(也许工厂

应该实例化该类并返回该类?)但我认为其他人有

使用了这种设计,并为其命名。
I''m not wedded to the idea, there are alternatives (perhaps the factory
should instantiate the class and return that?) but I assume others have
used this design and have a name for it.



问题是,我不知道你为什么要使用一个类

来存储状态。


欢呼

詹姆斯


-

-

- 问题通过方法解决

The problem is, I don''t see why you''re using a class
to store state in the first place.

cheers
James

--
--
-- "Problems are solved by method"


这篇关于你怎么称呼一个不打算实例化的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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