-1

In python, how to call a function C nested under another function B inside a class A from outside of the class.

>>> class A:
...    def funcB(self):
...       print('inside funcB')
...       def funcC(self):
...          print('hello world')
...
>>> a=A()
>>> c=a.funcB()
inside funcB
>>> c=a.funcB.funcC()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'funcC'

1 Answers1

1

This is just like asking how can I read the value of a variable defined inside a function, from outside the function.

You can't. The inner variable only exists when the function is called; and once the function returns, the inner variable is discarded.

It works the same for anything defined within a function; a string, a function, a variable, etc.

The only way to "call it" would be to return it from the function.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284