Re: [Tutor] question about function inside of function

2010-01-10 Thread Kent Johnson
On Sat, Jan 9, 2010 at 8:03 AM, spir  wrote:

> Do you realize the inner func will be redefined before each call? Meaning in 
> your case n calls x n outer loops x n inner loops.
>   def f() ...
> is actually a kind of masked assignment
>   f = function()...

That's true, but it is pretty inexpensive. The heavy lifting is at
compile time when a code object is created. Wrapping this as a
function is fast.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about function inside of function

2010-01-09 Thread Richard D. Moores
On Sat, Jan 9, 2010 at 07:28, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>>
>> to be put in a function. For convenience sake, I've put this new
>> function inside the one that calls it.
>>
>> Question 1: Is this bad practice? It works fine that way, but..
>
> No, but there are some issues to consider.
> Denis has addressed some but one other is...
>
> Reuse: by hiding the function inside the outer one it means it can only ever
> be used inside that function.

That's OK. It's very specific to the enclosing function.

> It may be better to have it as a module level
> function - even if you use the _prefix to limit its use outside the module.
>
>> Question 2: If the answer to Q1 is no, is there a standard place to
>> put a function inside of another function? Is it standard to have it
>> the first line of the function? I've put mine at the point where it
>> would be called, mainly as a reminder to myself.
>
> Top of the function is best because as Denis says it only gets defined once
> per function call. If you are really paranoid and the function only gets
> used in exceptional cases then you could define it at the point of use but
> in that case you probably don't need a function at all!

I moved it to the top. But since writing it I debugged and edited the
script so that the small function is used in only one case. So you're
right, I don't need it as a function at all.

Thanks, Alan. Right on as usual.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about function inside of function

2010-01-09 Thread Alan Gauld


"Richard D. Moores"  wrote 


to be put in a function. For convenience sake, I've put this new
function inside the one that calls it.

Question 1: Is this bad practice? It works fine that way, but..


No, but there are some issues to consider.
Denis has addressed some but one other is...

Reuse: by hiding the function inside the outer one it means it 
can only ever be used inside that function. It may be better to 
have it as a module level function - even if you use the _prefix 
to limit its use outside the module.



Question 2: If the answer to Q1 is no, is there a standard place to
put a function inside of another function? Is it standard to have it
the first line of the function? I've put mine at the point where it
would be called, mainly as a reminder to myself.


Top of the function is best because as Denis says it only gets 
defined once per function call. If you are really paranoid and 
the function only gets used in exceptional cases then you could 
define it at the point of use but in that case you probably don't 
need a function at all!


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about function inside of function

2010-01-09 Thread Richard D. Moores
On Sat, Jan 9, 2010 at 05:03, spir  wrote:

> Do you realize the inner func will be redefined before each call?

Oh, I forgot about that. Thanks!

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about function inside of function

2010-01-09 Thread spir
Richard D. Moores dixit:

> I'm working on a function that seems to cry out for some of its code
> to be put in a function. For convenience sake, I've put this new
> function inside the one that calls it.
> 
> Question 1: Is this bad practice? It works fine that way, but..
> 
> Question 2: If the answer to Q1 is no, is there a standard place to
> put a function inside of another function? Is it standard to have it
> the first line of the function? I've put mine at the point where it
> would be called, mainly as a reminder to myself.
> 
> An outline of the big function is:
> 
> if:
> else:
>  for loop
>  for loop
>  if
>  elif
>  the function
>  call the function
>  else
> 
> Thanks,

Do you realize the inner func will be redefined before each call? Meaning in 
your case n calls x n outer loops x n inner loops.
   def f() ...
is actually a kind of masked assignment
   f = function()...

To avoid polluting the global namespace (if it's what you mean), you can 
assiattach the inner func as an attribute of the outer: "g.f = f". 
Unfortunately, it's not possible (I guess), to define it directly using "def 
g.f()".
Also, if a func logically is an attribute of another, then probably you're 
close to defined an object, possibly with other attributes. Think at the design 
of the application.

I may be wrong, but I guess the only common case where you should (and must) 
define a func inside another is the one of a func factory, ie a func that 
defines (and returns) a new func:

def makeAdder(val):
def adder(x):
return x + adder.val
adder.val = val
return adder
add3 = makeAdder(3)
print add3(2)   # --> 5

Denis


la vita e estrany

http://spir.wikidot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] question about function inside of function

2010-01-09 Thread Richard D. Moores
I'm working on a function that seems to cry out for some of its code
to be put in a function. For convenience sake, I've put this new
function inside the one that calls it.

Question 1: Is this bad practice? It works fine that way, but..

Question 2: If the answer to Q1 is no, is there a standard place to
put a function inside of another function? Is it standard to have it
the first line of the function? I've put mine at the point where it
would be called, mainly as a reminder to myself.

An outline of the big function is:

if:
else:
 for loop
 for loop
 if
 elif
 the function
 call the function
 else

Thanks,

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor