存储函数所需的对象。 [英] Storing objects required by functions.

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

问题描述

除了我在这里发表的最后一篇文章之外,我还在构建一个

正则表达式对象并将其存储在某处以供

函数内部使用。我对我的任何解决方案都不满意:

#我不喜欢这个,但事实上你可以修改程序'

#通过命名参数的函数看起来很整洁。


def uses_default_parm_yuck(x,r = re.compile(" ...")):

通过

g = re.compile(''''')


def uses_global_yuck(x):

全球g

通过

#这太可怕了,可能很慢。


class is_hex:

def __init __(自我):

self.r = re.compile(''''')


def __call __(self, x):

r = self.r

通过


is_hex = is_hex()

#这会破坏范围,以便您的程序无法正常访问它'

#parent scope。因为我从来没有这样做过,

#这是我最喜欢的。


def is_hex():

r = re。编译('''''')

def is_hex(s):

返回r.match(s)不是没有

返回is_hex


is_hex = is_hex()


我错过了什么吗?这样做有更好的方法吗?在一天

到一天的基础上,我发现自己经常处于这种情况,现在我想到了它,我想要的是,这是我想要的

改进。


有趣的是,在私下里它从来没有打扰过我,但是当你在comp.lang上张贴时发表了b $ b。 python我发现代码是不可接受的。

也许我有两种编程模式,理想主义和实用? *耸肩*

:)

大卫。

Further to my last post here, I was playing some more with building a
regex object and storing it somewhere for use internally by a
function. I''m not happy with any of my solutions:
# I don''t like this, but the fact that you can modify the procedure''s
# function via a named argument seems neat in a hacky sort of way.

def uses_default_parm_yuck(x, r = re.compile("...")):
pass
g = re.compile(''...'')

def uses_global_yuck(x):
global g
pass
# This is horrible and probably slow.

class is_hex:
def __init__(self):
self.r = re.compile(''...'')

def __call__(self, x):
r = self.r
pass

is_hex = is_hex()
# This mucks up scoping so that your procedure can''t access it''s
# parent scope like it could normally. Since I never do this,
# it''s my favourite.

def is_hex():
r = re.compile(''...'')
def is_hex(s):
return r.match(s) is not None
return is_hex

is_hex = is_hex()

Am I missing something? Is there a nicer way of doing this? On a day
to day basis I find myself in this situation quite regularly, and now
I come to think of it today, it is something that I would like to
improve.

It is funny that in private it has never bothered me much, but when
posting on comp.lang.python I find that the code is unacceptable.
Maybe I have two modes of programming, idealist and practical? *shrug*
:)
David.

推荐答案



David M. Wilson < DW *********** @ botanicus.net>在留言中写道

news:99 ************************** @ posting.google.c om ...

"David M. Wilson" <dw***********@botanicus.net> wrote in message
news:99**************************@posting.google.c om...
除了我在这里发表的最后一篇文章之外,我还在构建一个
正则表达式对象并将其存储在某个地方以供
函数内部使用。我对我的任何解决方案都不满意:

#我不喜欢这个,但事实上你可以通过命名修改程序'
#功能争论看起来很整洁。

def uses_default_parm_yuck(x,r = re.compile(" ...")):
传递

g = re.compile(''...'')

def uses_global_yuck(x):
全球g
传递

#这个很糟糕,可能很慢。

类is_hex:
def __init __(self):
self.r = re.compile(''''')

def __call __(self,x):
r = self.r
传递

is_hex = is_hex()

#这个挖掘范围,以便你的程序无法正常访问它的#parent范围。由于我从不这样做,
#这是我的最爱。

def is_hex():
r = re.compile('''''')
def is_hex(s):
返回r.match(s)不是没有
返回is_hex

is_hex = is_hex()

我错过了什么吗?这样做有更好的方法吗?在一天的基础上,我经常发现自己处于这种情况,现在我今天想起来,这是我想要改进的事情。

有趣的是,在私下它从来没有打扰过我,但是当在comp.lang.python上发帖时,我发现代码是不可接受的。
也许我有两种编程模式,理想主义和实用? *耸肩*
:)


你的第二个解决方案(你标记的那个可怕而且可能是

慢)是一个经典的例子。一个函数对象,它在设计模式中以这种方式描述了

。 AFAIK,它有相同的通话开销

作为任何其他方式的工作。我会称之为最简单的方式来实现

a参数化功能。


John Roth

大卫。
Further to my last post here, I was playing some more with building a
regex object and storing it somewhere for use internally by a
function. I''m not happy with any of my solutions:
# I don''t like this, but the fact that you can modify the procedure''s
# function via a named argument seems neat in a hacky sort of way.

def uses_default_parm_yuck(x, r = re.compile("...")):
pass
g = re.compile(''...'')

def uses_global_yuck(x):
global g
pass
# This is horrible and probably slow.

class is_hex:
def __init__(self):
self.r = re.compile(''...'')

def __call__(self, x):
r = self.r
pass

is_hex = is_hex()
# This mucks up scoping so that your procedure can''t access it''s
# parent scope like it could normally. Since I never do this,
# it''s my favourite.

def is_hex():
r = re.compile(''...'')
def is_hex(s):
return r.match(s) is not None
return is_hex

is_hex = is_hex()

Am I missing something? Is there a nicer way of doing this? On a day
to day basis I find myself in this situation quite regularly, and now
I come to think of it today, it is something that I would like to
improve.

It is funny that in private it has never bothered me much, but when
posting on comp.lang.python I find that the code is unacceptable.
Maybe I have two modes of programming, idealist and practical? *shrug*
:)
Your second solution (the one you labeled "horrible and probably
slow") is a classic example of a Function Object, and it''s described
that way in "Design Patterns". AFAIK, it''s got the same call overhead
as any other way of doing the job. I''d call it the cleanest way of doing
a parameterized function.

John Roth

David.



在文章< 99 ************************** @ posting.google.google .com>,

David M. Wilson< dw *********** @ botanicus.net>写道:
In article <99**************************@posting.google.com >,
David M. Wilson <dw***********@botanicus.net> wrote:

g = re.compile(''...'')

def uses_global_yuck(x):
全球g
传递

g = re.compile(''...'')

def uses_global_yuck(x):
global g
pass




为什么不使用全局?没有``global``声明,那就是。

不喜欢这样吗?与使用类实例有什么不同?

-

Aahz(aa**@pythoncraft.com)< *> http://www.pythoncraft.com/


温伯格的第二定律:如果建筑商按照程序员编写的方式建造建筑物,那么第一只啄木鸟会摧毁文明。



Why not just use the global? Without the ``global`` statement, that is.
Don''t like that? How''s it any different from using a class instance?
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Weinberg''s Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.



" Aahz" < AA ** @ pythoncraft.com>在消息中写道

news:bs ********** @ panix1.panix.com ...

"Aahz" <aa**@pythoncraft.com> wrote in message
news:bs**********@panix1.panix.com...
在文章< 99 **** **********************@posting.google.com> ;,
David M. Wilson< dw ******** ***@botanicus.net>写道:
In article <99**************************@posting.google.com >,
David M. Wilson <dw***********@botanicus.net> wrote:

g = re.compile(''...'')

def uses_global_yuck(x):
全球g
传递
为什么不直接使用全局?没有全球的声明,就是这样。
不是那样的吗?与使用类实例有什么不同?

g = re.compile(''...'')

def uses_global_yuck(x):
global g
pass
Why not just use the global? Without the ``global`` statement, that is.
Don''t like that? How''s it any different from using a class instance?




你不能参数化它。你有一个全局,而

你可以在不同的类实例中放入不同的参数。

他不是在这里做的,但那是'

功能对象设计模式闪耀的主要地方。


John Roth

-
Aahz(aa * *@pythoncraft.com)< *>



You can''t parameterize it. You''ve got one global, while
you can put different parameters in different class instances.
He isn''t doing that here, but that''s the major place where the
Function Object design pattern shines.

John Roth
--
Aahz (aa**@pythoncraft.com) <*>


http:// www.pythoncraft.com/


这篇关于存储函数所需的对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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