Ach! My sample was a simplified version of the file that is actually 
failing.

Here is the actual code. Explanatory comments on line 3 and line 43 
delineated with ## #. 
This fails every time on my system, Python 2.7.3 on Debian Wheezy. 

I wrote this to demonstrate the exact point made by Anthony above.

I'm sure I'm missing something, but I cannot see it. 


#! /var/bin python


## # Notice these two assignments
foo = 'bar'
i_am_global = 'Blue Parrot'


print i_am_global


class print_it(object):




    def __init__(self, some_string=None):
        self.some_string = some_string




    def print_the_global(self):
        """
        Not to start a comp sci flame war about
        classes with side effects,
        or multiple exit points,
        or about how some Python guy showed
        a bunch of spagetti code.
        """
        print 'I, print_it, am going to print the global var.'
        try:
            print i_am_global
        except:
            print 'Where is the global variable?'
        print 'I, print_it, am done. Thank you.'




    def change_the_global(self, new_value_for_global):
        i_am_global =  new_value_for_global


    def change_and_print_the_global(self, newval):
        i_am_global = newval
        print i_am_global




def main():




    ## # Now comes the exception in line 45
    print foo
    print i_am_global


    new_val = 'pwned'
    found_it = False


    print 'I think I found i_am_global in the global scope.'
    print 'I\'ll prove it by printing its contents.'
    print 'Watch.'
    print '\n'
    try:
        print 'It says, "{}"'.format(i_am_global)
    except Exception as e:
        print 'Oops, I got this Exception: %s' %e
        print Exception()
        print 'That didn\'t work so well.'
    else:
        found_it = True
        print 'There, see?'
    print '\n'


    # Here is the change
    if found_it:
        print 'Now I\'m going to change it to "pwned."'
        i_am_global = new_val
        print 'This is the result of my change.'
        print 'It now says "{}".'.format(i_am_global)


    print '\n'
    print 'Now for some object action.'
    printer_to_screen = print_it() #Created an instance.
    print 'This is my new object: {}'.format(printer_to_screen)
    print '\n'
    print 'My new object is going to print the global variable'


    printer_to_screen.print_the_global()
    print '\n'
    print 'how did that work out?'
    print '\n'
    print 'My new object is going to reassign the global variable'
    printer_to_screen.change_the_global('fubar')
    print 'Now to print it'
    printer_to_screen.print_the_global()


    print '\n'
    print 'How about if we change it and print it in the same method?'
    printer_to_screen.change_and_print_the_global('fubar')


    print '\n'
    print "Let's see if the change stuck"
    printer_to_screen.print_the_global()




if __name__== '__main__':
    main()
    print '\nHere is the final value of the global: {}'.format(i_am_global)




On Saturday, April 5, 2014 9:43:13 AM UTC-4, Anthony wrote:
>
> On Friday, April 4, 2014 5:51:11 PM UTC-4, Cliff Kachinske wrote:
>>
>> If I write a python module like this:
>>
>> # born_to_fail.py
>> foo = 'bar'
>> def main():
>>     print foo
>> if __name__=='__main__': main()
>>
>>
> The above shouldn't produce an error, but the following will:
>
> foo = 'bar'
> def main():
>     print foo
>     foo = 'foo'
>
> In the above, the assignment to foo indicates foo is a local variable 
> (since it hasn't been explicitly declared as global), so the previous print 
> statement raises an exception because it refers to a local variable that 
> has not yet been declared.
>
> In your original example, because there is no assignment to foo within the 
> function, the foo in the print statement is assumed to be global, and there 
> is no exception because foo is in fact a global variable.
>
> Anthony
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to