Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > nested functions

Reply
Thread Tools

nested functions

 
 
micklee74@hotmail.com
Guest
Posts: n/a
 
      04-14-2006
hi
just curious , if i have a code like this?

def a():
def b():
print "b"
def c():
print "c"

how can i call c() ??

 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      04-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hi
> just curious , if i have a code like this?
>
> def a():
> def b():
> print "b"
> def c():
> print "c"
>
> how can i call c() ??


c is a name in the local scope of a(). You can call c from within a,
where the name is in scope, or you can return c or in some other way
make the value available in some other scope:

In [5]: def a():
...: def c():
...: print 'called c'
...: c()
...: return c
...:

In [6]: cc=a()
called c

In [7]: cc()
called c

Kent
 
Reply With Quote
 
 
 
 
Szabolcs Berecz
Guest
Posts: n/a
 
      04-14-2006
On 14 Apr 2006 04:37:54 -0700, (E-Mail Removed)
<(E-Mail Removed)> wrote:
> def a():
> def b():
> print "b"
> def c():
> print "c"
>
> how can i call c() ??


Function c() is not meant to be called from outside function a().
That's what a nested function is for: localizing it's usage and
prevent cluttering the global namespace

Szabi
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-14-2006
(E-Mail Removed) wrote:

> just curious , if i have a code like this?
>
> def a():
> def b():
> print "b"
> def c():
> print "c"
>
> how can i call c() ??


in the same way as you'd access the variable "c" in this example:

def a():
c = 10

(that is, by calling the function and accessing the local variable "c"
from the inside. in both cases, "c" lives in the local namespace, and
doesn't exist at all unless you call the function).

</F>



 
Reply With Quote
 
Thomas Bartkus
Guest
Posts: n/a
 
      04-14-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> hi
> just curious , if i have a code like this?
>
> def a():
> def b():
> print "b"
> def c():
> print "c"
>
> how can i call c() ??


Your function 'a' is it's own little world where functions 'b' and 'c'
exist.
Your code inside 'a' can call 'b' or 'c' - neat as you please.

BUT 'b' and 'c' simply do not exist outside the 'a' world. This is perfect
because you are in control - building worlds according to your own design.
Had it not been your intention to hide 'b' and 'c', you would not have
isolated them in this manner inside of 'a' .

I, for one, am so glad to have nested functions again
Thomas Bartkus


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-14-2006
Thomas Bartkus wrote:

> I, for one, am so glad to have nested functions again


again ?

Python has always supported nested functions. it's the scoping rules
that have changed; before the change from LGB to LEGB, you had to
explictly import objects from outer scopes.

</F>



 
Reply With Quote
 
bruno at modulix
Guest
Posts: n/a
 
      04-14-2006
Szabolcs Berecz wrote:
> On 14 Apr 2006 04:37:54 -0700, (E-Mail Removed)
> <(E-Mail Removed)> wrote:
>
>>def a():
>> def b():
>> print "b"
>> def c():
>> print "c"
>>
>>how can i call c() ??

>
>
> Function c() is not meant to be called from outside function a().
> That's what a nested function is for: localizing it's usage and
> prevent cluttering the global namespace


There's actually more than this about Python's nested functions: they
can be returned from the outer function and then carry the environnement
in which they where created:

def trace(func):
fname = func.__name__

def traced(*args, **kw):
print "calling func %s with *%s, **%s" % (fname, str(args), kw)
try:
result = func(*args, **kw)
except Exception, e:
print "%s raised %s" % (fname, e)
raise
else:
print "%s returned %s" % (fname, str(result))
return result

return traced

def test(arg1, arg2='parrot'):
print "in test, arg1 is %s" % arg1
return arg2 * 3

test = trace(test)
test(42)


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-14-2006
In article <(E-Mail Removed)>,
"Thomas Bartkus" <(E-Mail Removed)> wrote:

>Your function 'a' is it's own little world where functions 'b' and 'c'
>exist.
>Your code inside 'a' can call 'b' or 'c' - neat as you please.
>
>BUT 'b' and 'c' simply do not exist outside the 'a' world.


It's worth distinguishing between the _names_ 'b' and 'c' and the
_functions_ referred to by those names. The _names_ certainly do not
exist outside of the scope of the function referred to by 'a' (any
occurrences of 'b' and 'c' outside that scope refer to _different_
names), but the _functions_ they refer to certainly do exist.
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-15-2006
Lawrence D'Oliveiro wrote:

> >BUT 'b' and 'c' simply do not exist outside the 'a' world.

>
> It's worth distinguishing between the _names_ 'b' and 'c' and the
> _functions_ referred to by those names. The _names_ certainly do not
> exist outside of the scope of the function referred to by 'a' (any
> occurrences of 'b' and 'c' outside that scope refer to _different_
> names), but the _functions_ they refer to certainly do exist.


that's a bit misleading. "def" is an executable statement, and it
*creates* a function object when it's executed. that object is
handled in exactly the same way as any other object.

in the following example, the function referred to by "c" doesn't
exist before a call to "a", and it doesn't exist after the function
has returned:

def a():
def c():
print "c"
c()

if you call the function again, a new function object is created.

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments