这种编码风格不好的做法? [英] This coding style bad practise?
问题描述
大家好,
我创建了一个创建相对唯一ID字符串的类,现在我的
程序运行正常并且符合预期但不知何故我得到的感觉
我误用__repr__,因为我猜人们希望在一个实例中执行一个
函数而不是使用它的表示字符串
实例本身,你能详细说明你是否发现了这个糟糕的b
练习,如果是的话,那会是一个更好的方法吗?
>
TIA
-----脚本-----
Hi all,
I created a class which creates a relative unique id string, now my
program just works fine and as expected but somehow I get the feeling
that I misused the __repr__ since I guess people expect to ''execute'' a
function in an instance instead of using it''s representation string of
the instance itself, could you elaborate whether you find this bad
practice and if yes what would have been a better way to do it?
TIA
----- script -----
导入字符串
导入时间
class IDGenerator(object):
"""(serverID,subversion_length)
从服务器名称,日期时间戳和版本号创建一个ID。
调用实例返回使用__repr__类函数的ID
示例用法:
import string
import time
class IDGenerator(object):
"""(serverID,subversion_length)
Create an ID from the server name, datetimestamp and version number.
Calling the instance returns the ID using the __repr__ class function
Example usage:
>>> id = idgen(''01'',4)
>>> id 01_20060424_151903_1>>> id 01_20060424_151905_2>>> id 01_20060424_151905_3>>> id 01_20060424_151906_4>>> id 01_20060424_151907_1>>>
>>> id = idgen(04,100)
>>> id
>>> id = idgen(''01'',4)
>>> id 01_20060424_151903_1 >>> id 01_20060424_151905_2 >>> id 01_20060424_151905_3 >>> id 01_20060424_151906_4 >>> id 01_20060424_151907_1 >>>
>>> id = idgen(04,100)
>>> id
4_20060424_152043_001
"""
def __init __(self,serverID,subversion_length):
self.ID = str(serverID)
self.length = int(subversion_length)
fill_length = len(str(self.length))
def fill(number):
return(string.zfill(number,fill_length))
self.fill = fill
def __repr __(self):
#如果已达到颠覆长度或生成器没有被定义,(重新)定义它,否则返回
#subversion的下一个值。
尝试:
return_value = self.range_gen.next( )
除了:
self.range_gen =(范围内的数字的数字(1,self.length + 1))
return_value = self.range_gen.next()
#创建版本标记。
return_value = self.ID + \
time.strftime(" _%Y%m%d_%H%M%S_",time.gmtime())+ \
self.fill(return_value)
#和返回它。
返回(return_value)
4_20060424_152043_001
"""
def __init__(self,serverID,subversion_length):
self.ID = str(serverID)
self.length = int(subversion_length)
fill_length = len(str(self.length))
def fill(number):
return(string.zfill(number,fill_length))
self.fill = fill
def __repr__(self):
# If the subversion length has been reached or the generator has not
# been defined, (re)define it, otherwise return the next value of the
# subversion.
try:
return_value = self.range_gen.next()
except:
self.range_gen = ( number for number in range(1,self.length+1) )
return_value = self.range_gen.next()
# Create the version stamp.
return_value = self.ID +\
time.strftime("_%Y%m%d_%H%M%S_",time.gmtime())+\
self.fill(return_value)
# And return it.
return(return_value)
-----脚本-----
-
mph
----- script -----
--
mph
推荐答案
Martin P. Hellwigaécrit:
Martin P. Hellwig a écrit :
大家好,
我创建了一个创建一个相对唯一的id字符串的类,现在我的
程序运行正常并且符合预期但不知何故我感觉我错误地使用了__repr__,因为我猜人们期望在一个实例中''执行''
函数而不是使用它的实例本身的表示字符串,你能详细说明你是否找到了这个糟糕的实践,如果是的话会是什么是一个更好的方法吗?
Hi all,
I created a class which creates a relative unique id string, now my
program just works fine and as expected but somehow I get the feeling
that I misused the __repr__ since I guess people expect to ''execute'' a
function in an instance instead of using it''s representation string of
the instance itself, could you elaborate whether you find this bad
practice and if yes what would have been a better way to do it?
为什么不只是使用呼叫运算符?即:
Why not just use the call operator instead ? ie:
id = IDGenerator(...)
id()
01_20060424_151903_1 id()
id = IDGenerator(...)
id() 01_20060424_151903_1 id()
01_20060424_151905_2
01_20060424_151905_2
Martin P. Hellwig写道:
Martin P. Hellwig wrote:
大家好,
我创建了一个创建相对唯一id字符串的类,现在我的
程序运行正常期待但不知何故我感觉我误用了__repr__,因为我猜人们希望在一个实例中执行一个
函数而不是使用它的表示字符串
实例本身,你能否详细说明你是否发现了这种糟糕的实践?如果是的话,那会是一个更好的方法吗?
TIA
Hi all,
I created a class which creates a relative unique id string, now my
program just works fine and as expected but somehow I get the feeling
that I misused the __repr__ since I guess people expect to ''execute'' a
function in an instance instead of using it''s representation string of
the instance itself, could you elaborate whether you find this bad
practice and if yes what would have been a better way to do it?
TIA
而不是评论使用类的风格,我只建议使用
替代方案。
你可以使用一个生成器函数,在调用时产生下一个id;
至少是'
我是如何实现IDgenerator的。
>
干杯,
Keir。
Rather than comment on the style of using a class, I''ll just suggest an
alternative.
You can use a generator function, which yields the next id when called;
at least that''s
how I''d implement an IDgenerator.
Cheers,
Keir.
Bruno Desthuilliers写道:
Bruno Desthuilliers wrote:
Martin P. Hellwigaécrit:
Martin P. Hellwig a écrit :
我创建了一个创建相对唯一id字符串的类,现在我的
程序运行正常并且符合预期但不知何故我感觉到了我错误地使用了__repr__,因为我猜人们希望在一个实例中执行一个
函数,而不是使用它的实例本身的表示字符串,你能详细说明一下吗?找到这个糟糕的实践,如果是,那会是一个更好的方法吗?
I created a class which creates a relative unique id string, now my
program just works fine and as expected but somehow I get the feeling
that I misused the __repr__ since I guess people expect to ''execute'' a
function in an instance instead of using it''s representation string of
the instance itself, could you elaborate whether you find this bad
practice and if yes what would have been a better way to do it?
为什么不直接使用呼叫运算符呢?即:
Why not just use the call operator instead ? ie:
>>> id = IDGenerator(...)
>>> id()01_20060424_151903_1>>> id()
>>> id = IDGenerator(...)
>>> id() 01_20060424_151903_1 >>> id()
01_20060424_151905_2
01_20060424_151905_2
因为它会影响内置?
抱歉,无法抗拒:-)
干杯,
Carl Friedrch Bolz
because that shadows a builtin?
sorry, could not resist :-)
Cheers,
Carl Friedrch Bolz
这篇关于这种编码风格不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!