Re: namespaces, scoping and variables

2010-08-03 Thread Jean-Michel Pichavant

rantingrick wrote:

On Aug 2, 3:12 pm, Chris Hare ch...@labr.net wrote:
  
Also you should use 4 space indention and never use tabs. This is the
accepted way. 

Then ask yourself why tabs are still in python 3.
Nice troll by the way.

JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces, scoping and variables

2010-08-02 Thread Thomas Jollans
On 08/02/2010 09:33 PM, Chris Hare wrote:
 I am having a problem getting around this variable namespace thing.
 
 Consider these code bits
 
 File a.py
 from Tkinter import *
 import a1
 
 def doAgain():
   x = a1.Net()
   x.show(Again!)
 
 root = Tk()
 root.title(test)
 f = Frame(root,bg=Yellow)
 l = Button(root,text=window 1,command=doAgain)
 f.grid()
 l.grid()
 a = 5
 x = a1.Net()
 x.show(window 2)
 if __name__ == __main__:
   root.mainloop()
 
 File a1.py
 from Tkinter import *
 
 class Net:
   def __init__(self):
   self.window = Toplevel()
   def show(self,t):   
   self.l = Label(self.window,text=t)
   self.l.grid()
 button = Button(self.window, text=Again)
   button.bind(Button-1, self.Again)
 button2 = Button(self.window, text=Dismiss)
   button2.bind(Button-1, self.hide)
   button.grid()
   button2.grid()
   def Again(self,event):
   x = Net()
   x.show(a)
   def hide(self,event):
   self.window.destroy()
 
 
 When I run a.py, it imports a1.py and click on the Again button, I get the 
 error
 
 Exception in Tkinter callback
 Traceback (most recent call last):
   File 
 /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py,
  line 1410, in __call__
 return self.func(*args)
   File /Volumes/Development/py/a1.py, line 17, in Again
 x.show(a)
 NameError: global name 'a' is not defined
 
 I believe this is the expected behavior.  so my question is this -- how do I 
 tell the code in a1.py about the variable a, which exists in a.py?  Do I have 
 to pass it as part of the function call, or what?  using
 
 global a
 
 in a1.py doesn't change anything.
 
 since I am using SQLite for the disk database, I was thinking I could keep 
 all the global variables in an in memory database and just access them when 
 I need to, but other ideas are welcome.

global in Python isn't the same as global in C, or in PHP.

Global is, in essence, a shorter way of saying within the scope of
this module -- which keeps the global nice and clean.

You should probably just pass in the object you call a when creating
the object that uses it, or when calling the function/method when
calling it. If you don't want to do that, you can simply import the
module where your global data is stored -- beware of from XYZ import
..., though - that copies the variables, and won't do you much good here.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces, scoping and variables

2010-08-02 Thread Dave Angel

Chris Hare wrote:

I am having a problem getting around this variable namespace thing.

Consider these code bits

File a.py
from Tkinter import *
import a1

def doAgain():
x =1.Net()
x.show(Again!)

root =k()
root.title(test)
f =rame(root,bg=Yellow)
l =utton(root,text=window 1,command=doAgain)
f.grid()
l.grid()
a =
x =1.Net()
x.show(window 2)
if __name__ =__main__:
root.mainloop()

File a1.py
from Tkinter import *

class Net:
def __init__(self):
self.window =oplevel()
def show(self,t):   
self.l =abel(self.window,text=t)
self.l.grid()
button =utton(self.window, text=Again)
button.bind(Button-1, self.Again)
button2 =utton(self.window, text=Dismiss)
button2.bind(Button-1, self.hide)
button.grid()
button2.grid()
def Again(self,event):
x =et()
x.show(a)
def hide(self,event):
self.window.destroy()


When I run a.py, it imports a1.py and click on the Again button, I get the error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py,
 line 1410, in __call__
return self.func(*args)
  File /Volumes/Development/py/a1.py, line 17, in Again
x.show(a)
NameError: global name 'a' is not defined

I believe this is the expected behavior.  so my question is this -- how do I 
tell the code in a1.py about the variable a, which exists in a.py?  Do I have 
to pass it as part of the function call, or what?  using

global a

in a1.py doesn't change anything.

since I am using SQLite for the disk database, I was thinking I could keep all the 
global variables in an in memory database and just access them when I need 
to, but other ideas are welcome.

Thanks,
Chris


  
First rule is never have circular referencing between modules.  In other 
words, since a.py imports a1.py, a.py can refer to things in a1.py, but 
never the other way around.  Any time you need to look backwards, find 
another means.


One approach is to create another module c.py as a container to hold 
those things that both a and a1 need.  That way they both import c, and 
there's no problem.


Another approach is to pass the global from a.py into a1.py, and use it 
that way.


And since you only have these two modules, you could just define it in 
a1.py, and reference it from a.py as

  a1.a

I would point out that using the same name for a module and a global 
variable is bad practice.  it certainly makes it hard to describe in 
this case.


HTH,
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces, scoping and variables

2010-08-02 Thread MRAB

Chris Hare wrote:

I am having a problem getting around this variable namespace thing.

Consider these code bits

File a.py
from Tkinter import *
import a1

def doAgain():
x = a1.Net()
x.show(Again!)

root = Tk()
root.title(test)
f = Frame(root,bg=Yellow)
l = Button(root,text=window 1,command=doAgain)
f.grid()
l.grid()
a = 5
x = a1.Net()
x.show(window 2)
if __name__ == __main__:
root.mainloop()

File a1.py
from Tkinter import *

class Net:
def __init__(self):
self.window = Toplevel()
def show(self,t):   
self.l = Label(self.window,text=t)
self.l.grid()
button = Button(self.window, text=Again)
button.bind(Button-1, self.Again)
button2 = Button(self.window, text=Dismiss)
button2.bind(Button-1, self.hide)
button.grid()
button2.grid()
def Again(self,event):
x = Net()
x.show(a)
def hide(self,event):
self.window.destroy()


When I run a.py, it imports a1.py and click on the Again button, I get the error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py,
 line 1410, in __call__
return self.func(*args)
  File /Volumes/Development/py/a1.py, line 17, in Again
x.show(a)
NameError: global name 'a' is not defined

I believe this is the expected behavior.  so my question is this -- how do I 
tell the code in a1.py about the variable a, which exists in a.py?  Do I have 
to pass it as part of the function call, or what?  using

global a

in a1.py doesn't change anything.

since I am using SQLite for the disk database, I was thinking I could keep all the 
global variables in an in memory database and just access them when I need 
to, but other ideas are welcome.


Why in a database? If you need the modules to share it then you could
put it in a shared module and refer to it there:

File a.py
-
import my_globals
...
my_globals.a = 5


File a1.py
--
import my_globals
...
x.show(my_globals.a)
--
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces, scoping and variables

2010-08-02 Thread Ethan Furman

Chris Hare wrote:

I am having a problem getting around this variable namespace thing.

Consider these code bits

File a.py
from Tkinter import *
import a1

def doAgain():
x = a1.Net()
x.show(Again!)

root = Tk()
root.title(test)
f = Frame(root,bg=Yellow)
l = Button(root,text=window 1,command=doAgain)
f.grid()
l.grid()
a = 5
x = a1.Net()
x.show(window 2)
if __name__ == __main__:
root.mainloop()

File a1.py
from Tkinter import *

class Net:
def __init__(self):
self.window = Toplevel()
def show(self,t):   
self.l = Label(self.window,text=t)
self.l.grid()
button = Button(self.window, text=Again)
button.bind(Button-1, self.Again)
button2 = Button(self.window, text=Dismiss)
button2.bind(Button-1, self.hide)
button.grid()
button2.grid()
def Again(self,event):
x = Net()
x.show(a)
def hide(self,event):
self.window.destroy()


When I run a.py, it imports a1.py and click on the Again button, I get the error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py,
 line 1410, in __call__
return self.func(*args)
  File /Volumes/Development/py/a1.py, line 17, in Again
x.show(a)
NameError: global name 'a' is not defined

I believe this is the expected behavior.  so my question is this -- how do I 
tell the code in a1.py about the variable a, which exists in a.py?  Do I have 
to pass it as part of the function call, or what?  using

global a

in a1.py doesn't change anything.


The global keyword does not make a variable global.  It tells the 
interpreter that the variable in question can be find in the module 
scope, not the function/method scope.  In other words, the variable is 
global to the module, but not to the whole program.


What you'll need to do is pass a into Net when you instanciate it, like 
so (untested):


  def doAgain():
  x = a1.Net(a)
  x.show(Again!)

and in Net:

class Net:
def __init__(self, some_number):
self.some_number = some_number
self.window = Toplevel()
.
.
.
def Again(self,event):
x = Net(self.some_number)
x.show()

Keep in mind, though, that if you change a in a.py after you've 
instanciated Net, your Net instance will not see the change.  For the 
change to show up, a would need to be mutable, and you would have to 
mutate it.  The other option is to change the Net instance's some_number 
directly (i.e. x.some_number = 9).


Hope this helps.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces, scoping and variables

2010-08-02 Thread Chris Hare
Thanks to everyone for answering my question.  I think its clear now.  I'll 
just go the stuff 'em in a module and import that route.

Chris

On Aug 2, 2010, at 3:03 PM, MRAB wrote:

 Chris Hare wrote:
 I am having a problem getting around this variable namespace thing.
 Consider these code bits
 File a.py
 from Tkinter import *
 import a1
 def doAgain():
  x = a1.Net()
  x.show(Again!)
 root = Tk()
 root.title(test)
 f = Frame(root,bg=Yellow)
 l = Button(root,text=window 1,command=doAgain)
 f.grid()
 l.grid()
 a = 5
 x = a1.Net()
 x.show(window 2)
 if __name__ == __main__:
  root.mainloop()
 File a1.py
 from Tkinter import *
 class Net:
  def __init__(self):
  self.window = Toplevel()
  def show(self,t):   
  self.l = Label(self.window,text=t)
  self.l.grid()
button = Button(self.window, text=Again)
  button.bind(Button-1, self.Again)
button2 = Button(self.window, text=Dismiss)
  button2.bind(Button-1, self.hide)
  button.grid()
  button2.grid()
  def Again(self,event):
  x = Net()
  x.show(a)
  def hide(self,event):
  self.window.destroy()
 When I run a.py, it imports a1.py and click on the Again button, I get the 
 error
 Exception in Tkinter callback
 Traceback (most recent call last):
  File 
 /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py,
  line 1410, in __call__
return self.func(*args)
  File /Volumes/Development/py/a1.py, line 17, in Again
x.show(a)
 NameError: global name 'a' is not defined
 I believe this is the expected behavior.  so my question is this -- how do I 
 tell the code in a1.py about the variable a, which exists in a.py?  Do I 
 have to pass it as part of the function call, or what?  using
 global a
 in a1.py doesn't change anything.
 since I am using SQLite for the disk database, I was thinking I could keep 
 all the global variables in an in memory database and just access them 
 when I need to, but other ideas are welcome.
 Why in a database? If you need the modules to share it then you could
 put it in a shared module and refer to it there:
 
 File a.py
 -
 import my_globals
 ...
 my_globals.a = 5
 
 
 File a1.py
 --
 import my_globals
 ...
   x.show(my_globals.a)
 -- 
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces, scoping and variables

2010-08-02 Thread rantingrick
On Aug 2, 3:12 pm, Chris Hare ch...@labr.net wrote:
 Thanks to everyone for answering my question.  I think its clear now.  I'll 
 just go the stuff 'em in a module and import that route.

Chris, first of all i want you to know that this message is not meant
to offend but it may offend you -- hopefully your a rational person
and can deal with criticism.

This code is horrible. What are you trying to do exactly? Is this
purely academic or are you actually using this code for a real
purpose? The Net class looks like a dialog. Are you wishing to
create a dialog? If so, then you would be better off subclassing
tkSimpleDialog.Dialog instead of rolling your own. Please explain what
you are trying to do from a users perspective so we can properly guide
you.

Also you should use 4 space indention and never use tabs. This is the
accepted way. Although Python does allow freedom i would suggest that
coding in a uniformly accepted manner is more productive for the
entire community. Google Python Style Guide for more info.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list