#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2

    So, why does this not work then?


    hey, does anyone know why this won't work for me?

    and the easiest possible way to make it work? (simple and short )

    when i run this code, I get "SyntaxError: 'return' outside function". I have also tried to leave out the ,test.__dict__ part, but still won't work for me.

    Code:
    #!/usr/bin/python3
    def test():
            exec("return",test.__dict__)
            
    test()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2

    think i might know why it doesnt work..


    it does not appear like this is supposed to work.

    Be aware that the return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.
    http:// docs.python.org /3.1/library/functions.html#exec

    now to work out a workaround.. :l
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2

    hmpf


    also, this does not seem to work... grrrr..

    the print part works fine, but the return part bring up the error: SyntaxError: invalid syntax

    Code:
    def test(someinput):
        someinput()
    
    test(print)
    test(return)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480
    Code:
    #!/usr/bin/python3
    def test():
        exec("return",test.__dict__)
    
    try:
        test()
    except SyntaxError:
        pass
    
    print('this works')
    Last edited by b49P23TIvg; January 8th, 2013 at 03:36 PM. Reason: insert code tags
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    I don't think it is possible to do what you're trying to do, but maybe if you could explain why you're trying to do it someone can come up with something similar?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2
    thanks for the help, after writing a detailed explanantion on what i want to do, I realised how I could achive a solution to the problem myself.. sometimes it helps to wirte things out loud ;-) (my solution is to have a while loop inside the function which depends on a boolean being true, and then change the value of the boolean instead... so simple really :S)

IMN logo majestic logo threadwatch logo seochat tools logo