[Tutor] visibility of variables

Kent Johnson kent37 at tds.net
Mon Aug 1 18:53:35 CEST 2005


Victor Reijs wrote:
> Hello all of you,
> 
> I have problems with the visibility of variables in python. I am
> used to the idea of Javascript and other languages where one can define
> global variables if they are defined outside a function. It seems Python
> is not doing that, but how can I make it work?
> 
> I think it needs some twist in my mind to get used to it, but I still
> don't grasp it. Can you help?

In function Do() you must add the declaration
  global gemetenkleur

See http://www.python.org/doc/faq/programming.html#how-do-you-set-a-global-variable-in-a-function and the entry that follows it for some explanation.

Kent

> 
> I have attached a program that is not functioning as I would like to
> have it functioning.
> It seems that the 'gementenkleur' in funct1 and funct2 are the value of
> gemetenkleur at the start of the program, but they don't chance after
> the definition of gemetenkleur=bmifrao1.bmp
> 
> I 'know' this is something particular to Python, by how can I work
> around this? I could add gemetenkleur in the argument list of the funct1
> and funct2 functions, but in my case I don't want this (funct1 will be a
> function to be used in scipy.fmin, and I have the idea that only the
> simplex can be as an argument).
> 
> Is there a good page on the web that described this visibility issue (or
> does it have a different name?) of Python? The delveintopython.pdf does
> not help me and also a tutorial of hetland.org (Instant python:
> instant-python.php.htm ) seems not to speak the right language for me;-).
> 
> Hope someone can help me. I have the idea this is essential to
> understand before continuing more in Python.
> 
> 
> All the best,
> 
> 
> Victor
> 
> 
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> bmifrao1bmp=[(41, 37, 33), (63, 56, 53), (107, 97, 92), (228, 226, 222), (81, 64, 107), (107, 131, 82), (236, 207, 71), (158, 58, 42)]
> print 'original bmifrao1bmp ',bmifrao1bmp
> #gemetenkleur=[(41, 37, 33), (63, 56, 53), (107, 97, 92), (228, 226, 222), (81, 64, 107), (107, 131, 82), (236, 207, 71), (158, 58, 42)]
> gemetenkleur=[[47,46,47],[62,65,61],[116,114,114],[238,233,232],[65,62,116],[0,144,75],[245,211,0],[207,65,60]]
> endkleur=[[47,46,47],[62,65,61],[116,114,114],[238,233,232],[65,62,116],[0,144,75],[245,211,0],[207,65,60]]
> 
> 
> def funct1():
>     print 'gemetenkleur in func1: ',gemetenkleur
>     a=funct2(gemetenkleur)
>     
> def funct2(kleuren):
>    print 'kleuren in funct2 (should be same as gemetenkleur): ',kleuren
>    return 1
> 
> def Do():
>    gemetenkleur=bmifrao1bmp[:]
>    print 'gemetenkleur has to be from now bmifrao1bmp: ',gemetenkleur    
>    funct1()
>    
> Do()   
>     
>   
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list