Python Forum
Can anyone explain the code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can anyone explain the code?
#1
How it works?

def externa(x):
    def interna(y):
        return y**x
    return interna
f1=externa(2)
print(f1(3)) #----> 9
print(f1(4))#----> 16
print('............')
f2=externa(4)
print(f2(3))#----> 81
print(f2(4))#----> 256
Reply
#2
This is a closure. In Python functions are first class function. A function inside a function have access to variables which are defined in the outer function. A function can return a function.

def foo():
    # local variable in function
    # foo
    greeting = 'Hello World'
    def bar():
        # access to local variable
        # greeting of
        # function foo
        print(greeting)
    # return the function
    # bar
    # without calling it
    return bar
When you call foo(), it returns the function bar. Then you can call the returned function, which has still access to the variable greeting.

inner_function = foo()
inner_function() => prints Hello World

or

foo()()

The statement def is an assignment for a function to a name.
You can write it with a lambda:

function_name = lambda x: x ** 2
function_name.__name__ = 'function_name'
Written with lambda with your example:
def externa(x):
    interna = lambda y: y**x
    # the variable x is not in the
    # function definition of interna
    # it's in the function definition of externa
    return interna
    # this returns interna
    # but does it not call it
I think you should have enough knowledge to understand what happens.
Outer function returns inner function. Inner function has access local variables of outer function.
A function can return a function. You can return a function without calling the function.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Never mind
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
You should see about the decorators. A decorator is a function which wraps another function and takes as an argument the function you want to decorate.
>>> def decorator(func):
...    
...     def wrapper(text):
...         print('Before')
...         func(text)
...         print('After')
...     return wrapper

>>> @decorator
... def say(text):
...     print(text)

>>> say('Hello')
Before
Hello
After
This way you can change the behaviour of any function and what it's doing.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Explain the python code in this definition Led_Zeppelin 1 734 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,001 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,078 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,103 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,230 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,492 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  What is the run time complexity of this code and please explain? samlee916 2 2,283 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,280 Oct-12-2020, 01:59 PM
Last Post: t4keheart
  Explain range in this code RavCOder 4 2,312 Oct-02-2019, 05:04 PM
Last Post: jefsummers
  Explain this code - for loop RavCOder 9 3,435 Sep-24-2019, 01:01 PM
Last Post: RavCOder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020