子类化循环控制号的int [英] Subclassing int for a revolving control number
问题描述
大家好!
我正在尝试将int子类化,这样一旦达到某个值,
就会翻回到起始点数。这用作X12
控制号。对int进行子类化并获得我想要的显示
这是微不足道的,但是最好的方法是这样:
990 + 1 = 991
991 + 1 = 992
....
998 + 1 = 999
999 + 1 = 001
我应该设置任何特殊功能,如__sub__来提高
NotImpemented只做我自己的自定义任务和__add__
函数? (其他功能在这个
上下文中没有任何意义......)
是否有正在改变的情况那种元功能? (就像GUI文本控件中的onChange
事件一样)
Chris
-
仍在搜索偶数素数> 2!
2004年7月16日星期五16:08:28 -0400,
Chris Cioffi< ev ********@gmail.com>写道:
大家好!
我正在尝试将int子类化,这样一旦达到某个值,它就会翻转回到起始计数。这用作X12
控制号。对int进行子类化并获得我想要的显示
这是微不足道的,但是最好的方法是:> 990 + 1 = 991
991 + 1 = 992
...
998 + 1 = 999
999 + 1 = 001
我应该设置任何特殊功能,如__sub__来提升
NotImpemented只是自己制作自定义分配和__add__
功能? (其他功能在这个
上下文中没有任何意义...)
是否有正在改变那种元功能? (就像GUI文本控件中的onChange
事件一样)
这听起来有点矫枉过正,至少对我的老式,极简主义来说
口味。请记住,Python有一个我们都是成年人。哲学。
未经测试:
class X12ControlNumber:
def __init __(self,initialvalue = 1):
self.x = initialvalue
def增量(个体经营):
self.x + = 1
如果自我.x == 1000
self.x = 1
def get(self):
返回self.x
#交替:
def __str __(自我):
返回''%03d''%self.x
x = X12ControlNumber()
打印x.get()#打印1
x.increment()
打印x.get()#打印2 >
x.increment()
:
:
:
x.increment()
打印x.get()#打印998
x.increment()
打印x.get()#打印999
x.increment()
打印x.get()#打印1
问候,
Dan
-
Dan Sommers
< http://www.tombstonezero.net/dan/>
永远不要和au跳跃nicorn。
Quoth Chris Cioffi< ev ******** @ gmail.com>:
|我正在尝试将int子类化为一旦达到某个值,
|它翻转回到起始计数。这是用作X12
|控制序列号。子类化int并获得我想要的显示
|这是微不足道的,但是最好的方法是:
| 990 + 1 = 991
| 991 + 1 = 992
| ......
| 998 + 1 = 999
| 999 + 1 = 001
|
|我应该设置任何特殊功能,如__sub__来提高
| NotImpemented只是自己做自定义任务和__add__
|功能? (其他的功能在这个
|上下文中没有意义......)
请记住,不能改变价值。你需要的只是
1)控制对象的初始值(在__init__中)和
2)使__add__返回你的类型而不是int。
等级OI:
max = 16
def __init __(self,t):
如果t> self.max:
t = t - self.max
self.value = t
def __add __(self,k):
返回OI(self.value + k)
def __str __(自我):
返回str(self.value)
def __repr __(自我):
返回''OI(%s)''%self.value
def __cmp __(self,v):
返回cmp(self.value,v)
你可以实现一个可变计数器,但是对于大多数应用程序来说,int b $ b的工作方式不那么令人困惑 - 比如,这应该怎么办?
i = OI(1)
j = i
i + = 1
打印i,j
Donn Cave, do**@drizzle.com
" Chris Cioffi" < EV ******** @ gmail.com>在留言中写道
新闻:ma ************************************ * @ pytho n.org ...
我正在尝试将int子类化,这样一旦达到某个值,它就会翻转回起始计数。这是用作X12
控件号。
<剩余剪切...>
为什么这必须是子类int ?我认为你陷入了困惑is-a的基本对象陷阱。和is-implemented-using-a和is-implemented-using-a最终
将继承层次结构转换为语义扭曲。 Dan Sommers''
贴出的课程看起来可以满足您的需求,无需额外的行李。
- Paul
Hello all!
I''m trying to subclass int such that once it reaches a certain value,
it flips back to the starting count. This is for use as an X12
control number. Subclassing int and getting the display like I want
it was trivial, however what''s the best way to make it such that:
990 + 1 = 991
991 + 1 = 992
....
998 + 1 = 999
999 + 1 = 001
Should I set any of the special functions like __sub__ to raise
NotImpemented and just make my own custom assignment and __add__
functions? (The other fucntions would make no sense in this
context...)
Is there an "on change" kind of meta-function? (Like the onChange
event in a GUI text control)
Chris
--
Still searching for an even prime > 2!
On Fri, 16 Jul 2004 16:08:28 -0400,
Chris Cioffi <ev********@gmail.com> wrote:
Hello all!
I''m trying to subclass int such that once it reaches a certain value,
it flips back to the starting count. This is for use as an X12
control number. Subclassing int and getting the display like I want
it was trivial, however what''s the best way to make it such that:
990 + 1 = 991
991 + 1 = 992
...
998 + 1 = 999
999 + 1 = 001 Should I set any of the special functions like __sub__ to raise
NotImpemented and just make my own custom assignment and __add__
functions? (The other fucntions would make no sense in this
context...) Is there an "on change" kind of meta-function? (Like the onChange
event in a GUI text control)
That all sounds like overkill, at least to my old-fashioned, minimalist
tastes. Remember, too, that Python has a "we''re all adults" philosophy.
Untested:
class X12ControlNumber:
def __init__( self, initialvalue = 1 ):
self.x = initialvalue
def increment( self ):
self.x += 1
if self.x == 1000
self.x = 1
def get( self ):
return self.x
# alternately:
def __str__( self ):
return ''%03d'' % self.x
x = X12ControlNumber( )
print x.get( ) # prints 1
x.increment( )
print x.get( ) # prints 2
x.increment( )
:
:
:
x.increment( )
print x.get( ) # prints 998
x.increment( )
print x.get( ) # prints 999
x.increment( )
print x.get( ) # prints 1
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Never play leapfrog with a unicorn.
Quoth Chris Cioffi <ev********@gmail.com>:
| I''m trying to subclass int such that once it reaches a certain value,
| it flips back to the starting count. This is for use as an X12
| control number. Subclassing int and getting the display like I want
| it was trivial, however what''s the best way to make it such that:
| 990 + 1 = 991
| 991 + 1 = 992
| ...
| 998 + 1 = 999
| 999 + 1 = 001
|
| Should I set any of the special functions like __sub__ to raise
| NotImpemented and just make my own custom assignment and __add__
| functions? (The other fucntions would make no sense in this
| context...)
Do bear in mind that ints don''t change value. All you need is
1) control the initial value of the object (in __init__) and
2) make __add__ return your type instead of int.
class OI:
max = 16
def __init__(self, t):
if t > self.max:
t = t - self.max
self.value = t
def __add__(self, k):
return OI(self.value + k)
def __str__(self):
return str(self.value)
def __repr__(self):
return ''OI(%s)'' % self.value
def __cmp__(self, v):
return cmp(self.value, v)
You could implement a mutable counter, but for most applications
the way int works is less confusing - like, what should this do?
i = OI(1)
j = i
i += 1
print i, j
Donn Cave, do**@drizzle.com
"Chris Cioffi" <ev********@gmail.com> wrote in message
news:ma*************************************@pytho n.org...
I''m trying to subclass int such that once it reaches a certain value,
it flips back to the starting count. This is for use as an X12
control number.
<remaining snipped...>
Why does this have to subclass int? I think you are falling into the basic
object trap of confusing "is-a" and "is-implemented-using-a" that eventually
turns inheritance hierarchies into a semantic contortion. Dan Sommers''
posted class looks to do just what you need, with no extra int baggage.
-- Paul
这篇关于子类化循环控制号的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!