[Python-es] namespaces y variables globales

Manuel E. Gutierrez megutierrez en uci.cu
Mie Nov 3 21:27:19 CET 2010


Y que tal esto:


#!/usr/bin/env python
#-*- coding: utf-8 -*-
from optparse import OptionParser

class ClaseGlobal(object):
	a = 0

def test_func():
    print ClaseGlobal.a + 2

def main():
    parser = OptionParser()
    parser.add_option('-t','--run_test', action='store_true', dest='run', \
        help='Run test',default=False)
    parser.add_option('-n', '--number', help='Number', dest='number', \
        action='store', type='int', default=None)
    options, args = parser.parse_args()

    if not options.number:
        ClaseGlobal.a = 1
    else:
        ClaseGlobal.a = options.number
    
    if options.run:
        #global a
        test_func()

if __name__ == '__main__':
    main()


Más información sobre la lista de distribución Python-es