Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12075 > unrolled thread
| Started by | smith jack <thinke365@gmail.com> |
|---|---|
| First post | 2011-08-23 19:59 +0800 |
| Last post | 2011-08-27 23:51 -0500 |
| Articles | 7 on this page of 47 — 21 participants |
Back to article view | Back to comp.lang.python
is there any principle when writing python function smith jack <thinke365@gmail.com> - 2011-08-23 19:59 +0800
Re: is there any principle when writing python function Peter Otten <__peter__@web.de> - 2011-08-23 14:20 +0200
Re: is there any principle when writing python function Roy Smith <roy@panix.com> - 2011-08-23 08:56 -0400
Re: is there any principle when writing python function Mel <mwilson@the-wire.com> - 2011-08-23 08:53 -0400
Re: is there any principle when writing python function Roy Smith <roy@panix.com> - 2011-08-23 08:55 -0400
Re: is there any principle when writing python function Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-08-23 15:00 +0200
Re: is there any principle when writing python function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-24 01:22 +1000
Re: is there any principle when writing python function Terry Reedy <tjreedy@udel.edu> - 2011-08-23 14:29 -0400
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-23 13:22 -0700
Re: is there any principle when writing python function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-24 11:44 +1000
Re: is there any principle when writing python function Seebs <usenet-nospam@seebs.net> - 2011-08-23 16:53 +0000
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-23 10:02 -0700
Re: is there any principle when writing python function alex23 <wuwei23@gmail.com> - 2011-08-23 20:05 -0700
Re: is there any principle when writing python function alex23 <wuwei23@gmail.com> - 2011-08-23 20:08 -0700
Re: is there any principle when writing python function Red John <redjohn367@gmail.com> - 2011-08-24 16:29 -0700
Re: is there any principle when writing python function ting@thsu.org - 2011-08-25 22:20 -0700
Re: is there any principle when writing python function Roy Smith <roy@panix.com> - 2011-08-26 07:15 -0400
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-26 08:20 -0700
Re: is there any principle when writing python function John Gordon <gordon@panix.com> - 2011-08-26 15:40 +0000
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-26 11:05 -0700
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-27 07:45 +1000
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-26 15:26 -0700
Re: is there any principle when writing python function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 11:26 +1000
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-27 11:37 +1000
Re: is there any principle when writing python function Roy Smith <roy@panix.com> - 2011-08-27 12:41 -0400
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-28 02:57 +1000
Re: is there any principle when writing python function Emile van Sebille <emile@fenx.com> - 2011-08-27 10:27 -0700
Re: is there any principle when writing python function Ben Finney <ben+python@benfinney.id.au> - 2011-08-28 07:57 +1000
Re: is there any principle when writing python function Emile van Sebille <emile@fenx.com> - 2011-08-27 15:21 -0700
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-27 16:01 -0700
Re: is there any principle when writing python function Roy Smith <roy@panix.com> - 2011-08-27 19:09 -0400
Re: is there any principle when writing python function Stephen Hansen <me+list/python@ixokai.io> - 2011-08-27 16:27 -0700
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-28 03:31 +1000
Re: is there any principle when writing python function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-28 06:27 +1000
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-28 06:38 +1000
Re: is there any principle when writing python function Roy Smith <roy@panix.com> - 2011-08-27 17:09 -0400
Re: is there any principle when writing python function Tobiah <tobiah@teranews.com> - 2011-08-26 08:48 -0700
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-27 02:10 +1000
Re: is there any principle when writing python function Neil Cerutti <neilc@norwich.edu> - 2011-08-29 14:52 +0000
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-30 04:20 +1000
Re: is there any principle when writing python function Neil Cerutti <neilc@norwich.edu> - 2011-08-29 18:40 +0000
Re: is there any principle when writing python function Chris Angelico <rosuav@gmail.com> - 2011-08-30 05:02 +1000
For some value of “sing” (was: is there any principle when writing python function) Ben Finney <ben+python@benfinney.id.au> - 2011-08-30 08:17 +1000
Re: is there any principle when writing python function Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-08-29 23:20 -0700
Re: is there any principle when writing python function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 04:16 +1000
Re: is there any principle when writing python function rantingrick <rantingrick@gmail.com> - 2011-08-26 15:37 -0700
Re: is there any principle when writing python function harrismh777 <harmar@member.fsf.org> - 2011-08-27 23:51 -0500
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2011-08-29 18:40 +0000 |
| Message-ID | <9c24onF3ktU2@mid.individual.net> |
| In reply to | #12413 |
On 2011-08-29, Chris Angelico <rosuav@gmail.com> wrote: >> In my house, I'm dad. In my chorus, I'm Neil. In town I'm Neil >> Cerutti, and in the global scope I have to use a meaningless >> unique identifier. Hopefully no Python namespace ever gets that >> big. > > Chorus? Does that imply that you sing? Neat :) Wait... not all Python programmers sing? > What you have, I think, is a module named Cerutti, in which you > have a class of which Neil is an instance. Inside method > functions, you can be referenced by "self" (which is to code > what pronouns are to English); outside of them, you are > referred to as Neil; and outside the module, Cerutti.Neil is > the cleanest way to reference you. But your name is still Neil, > no matter how you're referenced. The problem with that scenario is that, in real life, there's more than one Cerutti.Neil, and they like to move around. ;) -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-08-30 05:02 +1000 |
| Message-ID | <mailman.553.1314644575.27778.python-list@python.org> |
| In reply to | #12417 |
On Tue, Aug 30, 2011 at 4:40 AM, Neil Cerutti <neilc@norwich.edu> wrote: > Wait... not all Python programmers sing? I do, and there seems to be more than coincidental overlap between musos and coders. > The problem with that scenario is that, in real life, there's > more than one Cerutti.Neil, and they like to move around. ;) Yes indeed; which means that your Cerutti module is in a package: from norwich import Cerutti It's always possible to make a locally-unique identifier into a more globally unique one by prepending another tag to it. Alternatively, you need to be duck-typed: you're the Neil Cerutti who writes code, and if some other Neil Cerutti is asked to write code, he will throw an exception. That's probably the easiest way to deal with it - but I don't know of a way to implement it in a coded way. Maybe all names actually point to lists of objects, and whenever you try to do something with a name, the system goes through the elements of the list until one doesn't fail? Going back to the original question, the length of function name required for it to be "meaningful" is, obviously, a variable quantity. But I think it's still reasonable to use that as a rule of thumb for dividing functions - if you can sanely name both halves, without putting the entire code into the function name, then you have a case for refactoring. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-08-30 08:17 +1000 |
| Subject | For some value of “sing” (was: is there any principle when writing python function) |
| Message-ID | <87ty8zsw00.fsf_-_@benfinney.id.au> |
| In reply to | #12417 |
Neil Cerutti <neilc@norwich.edu> writes: > On 2011-08-29, Chris Angelico <rosuav@gmail.com> wrote: > > Chorus? Does that imply that you sing? Neat :) > > Wait... not all Python programmers sing? All Python programmers sing. Some of them should not. -- \ “To be is to do” —Plato | `\ “To do is to be” —Aristotle | _o__) “Do be do be do” —Sinatra | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-08-29 23:20 -0700 |
| Message-ID | <mailman.563.1314685217.27778.python-list@python.org> |
| In reply to | #12417 |
On 29 Aug 2011 18:40:24 GMT, Neil Cerutti <neilc@norwich.edu> declaimed
the following in gmane.comp.python.general:
> On 2011-08-29, Chris Angelico <rosuav@gmail.com> wrote:
> >> In my house, I'm dad. In my chorus, I'm Neil. In town I'm Neil
> >> Cerutti, and in the global scope I have to use a meaningless
> >> unique identifier. Hopefully no Python namespace ever gets that
> >> big.
> >
> > Chorus? Does that imply that you sing? Neat :)
>
> Wait... not all Python programmers sing?
>
Some of us manage to croak when the Python squeezes...
<ribbit>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-27 04:16 +1000 |
| Message-ID | <4e57e2ff$0$29984$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #12227 |
Tobiah wrote:
>
>> Furthermore: If you are moving code out of one function to ONLY be
>> called by that ONE function then you are a bad programmer and should
>> have your editor taken away for six months. You should ONLY create
>> more func/methods if those func/methods will be called from two or
>> more places in the code. The very essence of func/meths is the fact
>> that they are reusable.
>
> While I understand and agree with that basic tenet, I think
> that the capitalized 'ONLY' is too strong. I do split out
> code into function for readability, even when the function
> will only be called from the place from which I split it out.
In other words, you disagree. Which is good, because the text you quote is
terrible advice, and it is ironic that the person you quote judges others
as bad programmers when his advice is so bad.
I can think of at least five reasons apart from re-use why it might be
appropriate to pull out code into its own function or method even if it is
used in one place only:
(1) Extensibility. Just earlier today I turned one method into three:
def select(self):
response = input(self)
if response:
index = self.find(response)
else:
index = self.default
return self.menuitems[index-1]
turned into:
def choose(self, response):
if response:
index = self.find(response)
else:
index = self.default
return self.menuitems[index-1]
def raw_select(self):
return input(self)
def select(self):
return self.choose(self.raw_select())
I did this so that subclasses could override the behaviour of each component
individually, even though the caller is not expected to call raw_select or
choose directly. (I may even consider making them private.)
(2) Testing. It is very difficult to reach into the middle of a function and
test part of it. It is very difficult to get full test coverage of big
monolithic blocks of code: to ensure you test each path through a big
function, the number of test cases rises exponentially. By splitting it
into functions, you can test each part in isolation, which requires much
less work.
(3) Fault isolation. If you have a 100 line function that fails on line 73,
that failure may have been introduced way back in line 16. By splitting the
function up into smaller functions, you can more easily isolate where the
failure comes from, by checking for violated pre- and post-conditions.
(4) Maintainability. It's just easier to document and reason about a
function that does one thing, than one that tries to do everything. Which
would you rather work with, individual functions for:
buy_ingredients
clean_kitchen_work_area
wash_vegetables
prepare_ingredients
cook_main_course
fold_serviettes
make_desert
serve_meal
do_washing_up
etc., or one massive function:
prepare_and_serve_five_course_meal
Even if each function is only called once, maintenance is simpler if the
code is broken up into more easily understood pieces.
(5) Machine efficiency. This can go either way. Code takes up memory too,
and it may be easier for the compiler to work with 1000 small functions
than 1 big function. I've actually seen somebody write a single function so
big that Python couldn't import the module, because it ran out of memory
trying to compile it! (This function was *huge* -- the source code was many
megabytes in size.) I don't remember the details, but refactoring the
source code into smaller functions fixed it.
On the other hand, if you are tight for memory, 1 big function may have less
overhead than 1000 small functions; and these days, with even entry level
PCs often having a GB or more of memory, it is rare to come across a
function so big that the size of code matters. Even a 10,000 line function
is likely to be only a couple of hundred KB in size:
>>> text = '\n'.join('print x+i' for i in range(1, 10001))
>>> code = compile(text, '', 'exec')
>>> sys.getsizeof(code.co_code) # size in bytes
90028
So that's four really good reasons for splitting code into functions, and
one borderline one, other than code re-use. There may be others.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | rantingrick <rantingrick@gmail.com> |
|---|---|
| Date | 2011-08-26 15:37 -0700 |
| Message-ID | <69798bf3-ce54-48e0-b08d-f5897862e57c@b20g2000vbz.googlegroups.com> |
| In reply to | #12236 |
On Aug 26, 1:16 pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> (3) Fault isolation. If you have a 100 line function that fails on line 73,
> that failure may have been introduced way back in line 16. By splitting the
> function up into smaller functions, you can more easily isolate where the
> failure comes from, by checking for violated pre- and post-conditions.
What's wrong Steven, are track backs too complicated for you?
############################################################
# START DUMMY SCRIPT
############################################################
def very_long_function():
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(object)
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
max(range(5))
#
print 'blah'
print 'blah'
print 'blah-blah'
very_long_function()
############################################################
# END DUMMY SCRIPT
############################################################
Traceback (most recent call last):
File "C:/Python27/test33333.py", line 48, in <module>
very_long_function()
File "C:/Python27/test33333.py", line 26, in very_long_function
max(object)
TypeError: 'type' object is not iterable
Oh the humanity!
[toc] | [prev] | [next] | [standalone]
| From | harrismh777 <harmar@member.fsf.org> |
|---|---|
| Date | 2011-08-27 23:51 -0500 |
| Message-ID | <PPj6q.58$5b2.45@newsfe03.iad> |
| In reply to | #12075 |
smith jack wrote:
> i have heard that function invocation in python is expensive, but make
> lots of functions are a good design habit in many other languages, so
> is there any principle when writing python function?
> for example, how many lines should form a function?
Once Abraham Lincoln was asked how long a man's legs should be. (Well,
he was a tall man and had exceptionally long legs... his bed had to be
specially made.)
Old Abe said, "A man's legs ought to be long enough to reach from his
body to the floor".
One time the Austrian Emperor decided that one of Wolfgang Amadeus
Mozart's masterpieces contained too many notes... when asked how many
notes a masterpiece ought to contain it is reported that Mozart
retorted, "I use precisely as many notes as the piece requires, not one
note more, and not one note less".
After starting the python interpreter import this:
import this
... study carefully. If you're not Dutch, don't worry if some of it
confuses you. ... apply liberally to your function praxis.
kind regards,
--
m harris
FSF ...free as in freedom/
http://webpages.charter.net/harrismh777/gnulinux/gnulinux.htm
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.python
csiph-web