Python Forum
accessing a second level nested function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: accessing a second level nested function (/thread-28906.html)



accessing a second level nested function - varsh - Aug-09-2020

def html_tag( tag ):

    def body( msg ):
        print( "hi! :", msg )

        def inn_body( msg1 ):
            print( "hola: ", msg1 )
     return inn_body

    return body
h1 = html_tag( "h1" )
h1( "varshini" )
h1( "subiksha" )
/// How do i access the inn_body()?


RE: accessing a second level nested function - Yoriz - Aug-09-2020

def html_tag(tag):

    def body(msg):
        print("hi! :", msg)

        def inn_body(msg1):
            print("hola: ", msg1)

        return inn_body

    return body


h1 = html_tag("h1")
h1("varshini")
h1("subiksha")('inn_body')
Output:
hi! : varshini hi! : subiksha hola: inn_body



RE: accessing a second level nested function - deanhystad - Aug-09-2020

I was going to (ok, did) ask why would you want to do such a thing. Is it to allow writing code like that used in Yoriz' example? Looks confusing to me.


RE: accessing a second level nested function - varsh - Aug-13-2020

(Aug-09-2020, 03:40 PM)deanhystad Wrote: I was going to (ok, did) ask why would you want to do such a thing. Is it to allow writing code like that used in Yoriz' example? Looks confusing to me.
No, I was getting a hang of closures concept in python and I wanted to know on how to access a nested fuction using the closure method.