创建实用功能的常用做法? [英] common practice for creating utility functions?

查看:45
本文介绍了创建实用功能的常用做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天简单介绍一下:是否常见(并且也是首选的,这是两个不同的东西!)来创建一个单独工作的功能

调用另一个函数?


示例:为了娱乐和锻炼,我正在创建一个程序,它接受报价

并将其转换为密码。现在我有四个功能:


convert_quote - 启动它的主要功能

make_code - 制作并返回密码

make_set - 从make_code调用,将引号转换为一组,因此每个

字母只能获得一个编码字母

test_sets - 确保字母不是'''分配给自己


所以我的第一个问题是:我应该为此制作一个密码学课程,

还是功能正常?如果是后者,那么回到我的原点:

我可以做这样的事情:


def convert_quote(报价):

返回make_code(引用)


或者让函数调用另一个函数没有意义吗?

Just a quickie for today: Is it common (and also preferred, which are
two different things!) to create a function that has the sole job of
calling another function?

Example: for fun and exercise, I''m creating a program that takes a quote
and converts it into a cryptogram. Right now I have four functions:

convert_quote -- the main function that starts it all
make_code -- makes and returns the cryptogram
make_set -- called from make_code, converts the quote into a set so each
letter gets only one coded letter
test_sets -- makes sure a letter isn''t assigned to itself

So my first question is this: should I make a Cryptogram class for this,
or are functions fine? If the latter, then back to my original point:
can I do something like this:

def convert_quote(quote):
return make_code(quote)

Or does it not make sense to have a function just call another function?

推荐答案

John Salerno< jo ****** @ NOSPAMgmail.com>写道:
John Salerno <jo******@NOSPAMgmail.com> writes:
所以我的第一个问题是:我应该为这个做一个Cryptogram类,还是功能正常?如果是后者,那么回到原来的点:我可以这样做:

def convert_quote(引用):
返回make_code(引用)
So my first question is this: should I make a Cryptogram class for
this, or are functions fine? If the latter, then back to my original
point: can I do something like this:

def convert_quote(quote):
return make_code(quote)




如果它能更清楚地表达你的意图,那就没关系。

为你的函数写一个函数也是正常的还没写完




some_quote = convert_quote(another_quote)


之后你写了convert_quote。它可能不会直到你

实际上写convert_quote你注意到它只是调用

另一个函数。如果发生这种情况,那就没事了。


另外,如果你不需要像OOP一样上课,你也不需要上课

实例的东西。但是,有时候,上课也可以说明你的意图。例如,如果你想这样做,那么创建一个类意味着它可以让多个密码在相同的时间内活动变得更加简单。


编程不只是为要执行的
计算机创建指令串。它也是文学因为你正试图将b
$ b传递给其他人阅读代码。


曾经有人问过着名的自行车赛冠军Eddy Merckx(他是

有点像1970年代的Lance Armstrong,还有一些)如何成为优秀的赛车手。他的回答是骑很多。这与

编码的方式相同。随着您越来越有经验,这些问题变得更容易为您自己回答。但总是,做任何有用的事情,如果你的代码变得凌乱或遇到技术障碍,问问自己有什么选择

方法可以避免这些障碍,从而形成一种什么样的感觉

do。



It''s fine to do that if it expresses your intentions more clearly.
It''s also normal to write a call for a function you haven''t written
yet:

some_quote = convert_quote(another_quote)

where you write convert_quote afterwards. It may not be til you
actually write convert_quote that you notice that it simply calls
another function. If that happens, it''s fine.

Also, you don''t need to make a class if you don''t need to do OOP-like
things with the instances. But, sometimes, making a class also
clarifies your intentions. For example, making a class means it
becomes much simpler to have multiple cryptograms active at the same
time, if you want to do that.

Programming is not just creating strings of instructions for a
computer to execute. It''s also "literary" in that you are trying to
communicate a program structure to other humans reading the code.

Someone once asked the famous bike racing champion Eddy Merckx (he was
sort of the Lance Armstrong of the 1970''s and then some) how to become
a good racer. His answer was "ride a lot". It''s the same way with
coding. As you get more experienced these questions get easier to
answer for yourself. But always, do whatever works, and if your code
gets messy or hits technical snags, ask yourself what alternative
approaches avoid those snags, and thereby develop a sense of what to
do.


2006年5月15日星期一18:26:01 GMT,

John Salerno< ;乔****** @ NOSPAMgmail.com>写道:
On Mon, 15 May 2006 18:26:01 GMT,
John Salerno <jo******@NOSPAMgmail.com> wrote:
所以我的第一个问题是:我应该为这个做一个Cryptogram类,还是功能正常? ...


也许我是老学校,但是我不打算上班,除非我是
最终会有多个实例(或者我被推到一个角落,

例如,一个GUI框架)。

...如果是后者,那么回到我原来的观点:我能做点什么
这样:
def convert_quote(quote):
return make_code(quote)


当然可以。或者,因为这是python,你也可以这样做:


convert_quote = make_quote

或者让函数调用另一个函数没有意义
功能?
So my first question is this: should I make a Cryptogram class for
this, or are functions fine? ...
Perhaps I''m "old school," but I don''t bother with classes unless I''m
going to end up with multiple instances (or I''m pushed into a corner by,
e.g., a GUI framework).
... If the latter, then back to my original point: can I do something
like this: def convert_quote(quote):
return make_code(quote)
Of course you can. Or, since this is python, you can also do this:

convert_quote = make_quote
Or does it not make sense to have a function just call another
function?




如果有一个很好的设计级别的原因(比如保留某些物品或

类别不知道其他人,或者为你认识的东西留出空间

稍后会添加),然后一个函数组成

完全没有其他函数调用。但是,如果你最终得到很多这些微小的功能,并且它们会持续多个开发周期,那么你可能会在你的设计中犯一个系统错误。


问候,

Dan


-

Dan Sommers
< http://www.tombstonezero.net/dan/>

我希望人们按字母顺序死亡。 - 我的妻子,系谱学家



If there''s a good design-level reason (like keeping certain objects or
classes unaware of others, or leaving room for something you know you
will add later), then there''s nothing wrong with a function consisting
solely of another function call. If you end up with a lot of those tiny
functions, though, and they persist through multiple development cycles,
then you may be making a systematic mistake in your design.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist


John Salerno写道:
John Salerno wrote:
...是否常见... [和首选]创建一个只有
调用另一个函数的函数?

示例:...密码。现在我有四个功能:

convert_quote - 启动它的主要功能
make_code - 制作并返回密码
make_set - 从make_code调用,转换为引用成套,所以每封
字母只能得到一封编码的字母
test_sets - 确保一封信没有分配给自己

所以我的第一个问题是:应该我为此制作了一个Cryptogram类,
或函数是否正常?
功能很好。如果他们想分享州,我会上课。

...我可以这样做:
def convert_quote(quote):
return make_code(quote) )
或者让函数调用另一个函数没有意义吗?
...Is it common ...[and preferred] to create a function that has the sole job of
calling another function?

Example: ... cryptogram. Right now I have four functions:

convert_quote -- the main function that starts it all
make_code -- makes and returns the cryptogram
make_set -- called from make_code, converts the quote into a set so each
letter gets only one coded letter
test_sets -- makes sure a letter isn''t assigned to itself

So my first question is this: should I make a Cryptogram class for this,
or are functions fine? Functions are just fine. I''d use a class if they wanted to share state.
... can I do something like this:
def convert_quote(quote):
return make_code(quote)
Or does it not make sense to have a function just call another function?



显然你_can_那样做。但是,我不会。如果(对你而言)上面的四个

函数意味着不同的东西,我会实现convert_quote

与:

convert_quote = make_code


-

-Scott David Daniels
sc *********** @ acm。组织


这篇关于创建实用功能的常用做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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