473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Trouble with nested scopes when trying to rebind a variable

Hi,

I have a parameter defined in a module, called PREVIEW. Many functions use
it's value to modify their behavior.

A function called dispatch checks the user arguments in sys.argv and calls the
appropriate function. It should also modify the value of PREVIEW depending on
the user input.

And here's where I get into trouble: if I write something like PREVIEW = None
inside the body of a function, it doesn't modify the value of the existing
PREVIEW variable, it creates a new variable in the function scope.

How can I modify the external PREVIEW variable?

In Lisp there are different operators to rebind an existing variable and to
create a new variable. How do you do this in python?
Jul 18 '05 #1
2 1612
Fernando Rodriguez <fr*@easyjob.net> wrote in
news:18********************************@4ax.com:
And here's where I get into trouble: if I write something like
PREVIEW = None inside the body of a function, it doesn't modify the
value of the existing PREVIEW variable, it creates a new variable in
the function scope.

How can I modify the external PREVIEW variable?

In Lisp there are different operators to rebind an existing variable
and to create a new variable. How do you do this in python?


Use the global statement in the function to declare that you want to
modify the global variable rather than creating a local variable, see
section 6.13 of the reference manual. e.g.

PREVIEW = 0

def aFunction():
global PREVIEW
PREVIEW = 1

Better still, use classes and get rid of the global variable altogether.

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2
Fernando Rodriguez <fr*@easyjob.net> writes:
Hi,

I have a parameter defined in a module, called PREVIEW. Many functions use
it's value to modify their behavior.

A function called dispatch checks the user arguments in sys.argv and calls the
appropriate function. It should also modify the value of PREVIEW depending on
the user input.

And here's where I get into trouble: if I write something like PREVIEW = None
inside the body of a function, it doesn't modify the value of the existing
PREVIEW variable, it creates a new variable in the function scope.

How can I modify the external PREVIEW variable?
Just as you'd modify any variable, really (provided you're actually using a
*mutatable* datatype like a list),

PREVIEW[:] = []

However, what you actual seem to be after is globally reassigning the name
PREVIEW. so do:

def some_fun(bla):
global preview
preview = None

In Lisp there are different operators to rebind an existing variable and to
create a new variable. How do you do this in python?


You don't.

'as
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ......
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
8
by: Sean Givan | last post by:
Hi. I'm new to Python, and downloaded a Windows copy a little while ago. I was doing some experiments with nested functions, and ran into something strange. This code: def outer(): val =...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
4
by: jm.suresh | last post by:
def f(): a = 12 def g(): global a if a < 14: a=13 g() return a print f()
3
by: Dieter Maurer | last post by:
I met the following surprising behaviour .... for i in range(3): .... def gen1(): .... yield i .... yield i, gen1() .... .... 0 0 1 1
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.