Python Forum
Calling a function whch is inside a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a function whch is inside a function
#1
Here is the code for example:
def funcOuter():
    print("funcOuter() called")

    def funcInner():
        print("funcInner() called")

    print("bye bye!!!")
and i want to call funcInner(). If i call it as funcOuter().funcInner() then it is saying

Output:
'NoneType' object has no attribute '
Can anyone solve it for me?
Reply
#2
Good question but I am going to skirt around the answer because I believe inner functions are used in python because (as you found out) they are protected. At least, that's what I have gathered so far (also new to python). There kind of like a private function in that their functionality is protected from the global scope.

In other words there is essentially no good reason or time when you are going to want to use method nested within another method and if you did you wouldn't.. instead, you would just make a private method completely separate from the other. If your still interested:

def functOuter():
  print("funcOuter() called")

  def functInner():
    print("funcInner() called")

  functInner()

functOuter()
Reply
#3
(Aug-10-2018, 08:32 PM)Vysero Wrote: Good question but I am going to skirt around the answer because I believe inner functions are used in python because (as you found out) they are protected. At least, that's what I have gathered so far (also new to python). There kind of like a private function in that their functionality is protected from the global scope.

In other words there is essentially no good reason or time when you are going to want to use method nested within another method and if you did you wouldn't.. instead, you would just make a private method completely separate from the other. If your still interested:

def functOuter():
  print("funcOuter() called")

  def functInner():
    print("funcInner() called")

  functInner()

functOuter()

In your example, you are calling the inner function from the outer function, and it can be easily called because it in scope of it. It simply mean that we cannot call an inner function (from the class level scope or outer scope) because they are protected?
Reply
#4
protected is not precise. it imply that inner function still exists and is visible from outer/global scope, but not accessible (i.e. protected).
the scope of inner function is outer function. It is accessible only from within outer function, while outer function is executed, .
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Aug-11-2018, 05:37 AM)buran Wrote: protected is not precise. it imply that inner function still exists and is visible from outer/global scope, but not accessible (i.e. protected).
the scope of inner function is outer function. It is accessible only from within outer function, while outer function is executed, .

okay!! thanks Buran.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 604 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  calling external function with arguments Wimpy_Wellington 7 1,419 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 806 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  with open context inside of a recursive function billykid999 1 569 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 787 May-02-2023, 08:40 AM
Last Post: Gribouillis
Sad Iterate randint() multiple times when calling a function Jake123 2 2,033 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,803 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Exit function from nested function based on user input Turtle 5 2,889 Oct-10-2021, 12:55 AM
Last Post: Turtle
  [Solved] TypeError when calling function Laplace12 2 2,865 Jun-16-2021, 02:46 PM
Last Post: Laplace12
Question Stopping a parent function from a nested function? wallgraffiti 1 3,654 May-02-2021, 12:21 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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