-7

I want to call 1 precise function in another function from another function

def exemple():    
    def dostuff1():  
        print('Stuff 1')  
    def dostuff2():  
        print('Stuff 2')  
    def dostuff3():  
        print('Stuff 3')  

def training():
    #want to call ONLY Dostuff2
ZF007
  • 3,708
  • 8
  • 29
  • 48
Mayers
  • 1
  • 2
  • So dostuff2() is a function defined somewhere above. What is the problem with calling it in training()? – Mathieu Feb 26 '18 at 15:36
  • wrox - python 2.0 page 79! – ZF007 Feb 26 '18 at 15:44
  • 1
    @Psytho Dostuff is inside the scope of exemple function so he cannot access it directly. – gautamaggarwal Feb 26 '18 at 15:46
  • @gautamaggarwal The code is so broken so you cannot say anything about the scopes. – Psytho Feb 26 '18 at 15:54
  • @Psytho Yep you are right but he edited. So it becomes clear now. He wants to call a function from scope of another function. – gautamaggarwal Feb 26 '18 at 15:56
  • @Psytho i just forget some "def" before all "dostuff" sorry about that – Mayers Feb 26 '18 at 15:57
  • Why then you don't define this function outside and call it in exemple() and training()? – Psytho Feb 26 '18 at 15:59
  • @Psytho this is just an exemple to simplify the question, my real code is a tic tac toe game – Mayers Feb 26 '18 at 17:36
  • 1
    My comment was pretty clear about getting the basics onboard. Buy the book, shop at O'Reilly, Amazon or visit [zetcode](http://zetcode.com/lang/python/oop/) for tutorials on this specific core knowledge.The web is literately stockpiled with python tutorials about functions, classes and objects. Make your mistakes in how to call it from subclass, meta-class, nested functions, etc. Get your feet wet..because ..when you hit tk, PyQt5, GTK or verilog... you really need that foundation being onboard! And above all... get your kicks with modules, `self` and `if __name__ == main :`. You'll need it! – ZF007 Feb 27 '18 at 00:11
  • 1
    ..don't get me wrong... I and many others do want to help you (with pleasure)... but you can't run a marathon if you can't tie your shoelaces or be a winner if you run the 42k in more than 5 hours. Capiche! – ZF007 Feb 27 '18 at 00:19

2 Answers2

2

First, your syntax is not correct for inner functions: you must use def keyword as below:

def exemple():    
    def dostuff1():  
        print:('Stuff 1')  
    def dostuff2():  
        print:('Stuff 2')  
    def dostuff3():  
        print:('Stuff 3')

Secondly, this question has been already answered here: How to access a function inside a function?

in simple words, you can't call an inner function directly from outside, but you can do something like suggested by the answer from @gautamaggarwal.

Laurent H.
  • 6,316
  • 1
  • 18
  • 40
  • First, sorry for the syntac error, i wrote that fast for an exemple. its not my real code. second, the link dont answer my question cause its call ALL the other function, and i want to call only 1 function, not all of them – Mayers Feb 26 '18 at 15:49
  • Im new here and try to understand why people upvote you? you didnt really helped and just refer to the one that actually helped me.. people should upvote @gautamaggarwal not you.... – Mayers Feb 26 '18 at 17:24
0

Define your function example as this:-

def exemple(func_name):    
    def dostuff1():  
        print('Stuff 1')  
    def dostuff2():  
        print('Stuff 2')  
    def dostuff3():  
        print('Stuff 3')
    func_dic = {
        "dostuff1" : dostuff1,
        "dostuff2" : dostuff2,
        "dostuff3" : dostuff3
    }
    return func_dic[func_name]

Then call your function in the other function like this:

def training():
    exemple("dostuff2")()

I hope it helps!

gautamaggarwal
  • 341
  • 2
  • 11