Re: [Tutor] question about import statement
On Fri, 27 Aug 2010 02:18:10 pm Greg Bair wrote: yeah, from package import * doesn't actually import every name from a module. For example, by default, names starting with an underscore are not imported. Alternatively, if you have a variable named __all__ in your module, and it's a list of names, only those names in the list actually get imported when you do a from x import * Hugo Why would the person who wrote this package choose to do it this way, though? If it was something that people would use and not just an internal name, why hide it this way? I can't speak for the tkinter package, but in my own packages I like to distinguish between three broad categories of functions (and classes): * public functions that are part of the main functionality of the package; * public functions that are associated with the package, but not directly part of the package functionality (e.g. helper functions, support functions, unittests, experimental code that is provided but not supported, utilities, decorators, etc.); * private functions that I reserve the right to change or remove without notice. Only the first goes into __all__. Only the third start with an underscore. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
Bill Allen walle...@gmail.com wrote *from tkinter import * from tkinter import ttk These two lines tell Python that our program needs two modules. The first, tkinter, is the standard binding to Tk, which when loaded also causes the existing Tk library on your system to be loaded. The second, ttk, is Python's binding to the newer themed widgets that were added to Tk in 8.5. Yes, they are both modules so must both be imported. You could also do: import tkinter as tk import tkinter.ttk as ttk which is the style I tend to use for my own code. HTH, Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
Greg Bair wrote: On 08/26/2010 10:29 PM, Hugo Arts wrote: On Thu, Aug 26, 2010 at 9:16 PM, Bill Allen walle...@gmail.com wrote: I did try that and of course it gave an error because it was necessary. I just did not know why. However, I later found an explanation on the web. Here it is: from tkinter import * from tkinter import ttk These two lines tell Python that our program needs two modules. The first, tkinter, is the standard binding to Tk, which when loaded also causes the existing Tk library on your system to be loaded. The second, ttk, is Python's binding to the newer themed widgets that were added to Tk in 8.5. yeah, from package import * doesn't actually import every name from a module. For example, by default, names starting with an underscore are not imported. Alternatively, if you have a variable named __all__ in your module, and it's a list of names, only those names in the list actually get imported when you do a from x import * Hugo Why would the person who wrote this package choose to do it this way, though? If it was something that people would use and not just an internal name, why hide it this way? There are many programs out there that don't use ttk. For those it would be a waste of time and memory if the tkinter.ttk module were imported automatically. If you modify your example to from tkinter import * root = Tk() button = Button(root, text=Hello World).grid() root.mainloop() it will still work (but maybe look a little different). If the ttk import were automatic the above script might not work on machines that don't have themed widgets even though it doesn't use them. In general it is good practice that modules introduce as few dependencies as possible. Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
On Thu, Aug 26, 2010 at 7:44 PM, Corey Richardson kb1...@aim.com wrote: Why don't you try it out without the from tkinter import ttk statement, and see if it works? Bill Allen wrote: I was experimenting with Tk today, just trying it out. I found this example of a very simple hello world button click program. This is it. from tkinter import * from tkinter import ttk root = Tk() button = ttk.Button(root, text=Hello World).grid() root.mainloop() What I don't understand is why it is necessary to run that import statement twice. Shouldn't import * bring everything in from tkinter? Just wondering, Bill Allen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
On Thu, Aug 26, 2010 at 7:44 PM, Corey Richardson kb1...@aim.com wrote: Why don't you try it out without the from tkinter import ttk statement, and see if it works? Bill Allen wrote: I was experimenting with Tk today, just trying it out. I found this example of a very simple hello world button click program. This is it. from tkinter import * from tkinter import ttk root = Tk() button = ttk.Button(root, text=Hello World).grid() root.mainloop() What I don't understand is why it is necessary to run that import statement twice. Shouldn't import * bring everything in from tkinter? Oops, sorry about the previous empty post. I did try that and of course it gave an error because it was necessary. I just did not know why. However, I later found an explanation on the web. Here it is: *from tkinter import * from tkinter import ttk These two lines tell Python that our program needs two modules. The first, tkinter, is the standard binding to Tk, which when loaded also causes the existing Tk library on your system to be loaded. The second, ttk, is Python's binding to the newer themed widgets that were added to Tk in 8.5. * -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
On Thu, Aug 26, 2010 at 9:16 PM, Bill Allen walle...@gmail.com wrote: I did try that and of course it gave an error because it was necessary. I just did not know why. However, I later found an explanation on the web. Here it is: from tkinter import * from tkinter import ttk These two lines tell Python that our program needs two modules. The first, tkinter, is the standard binding to Tk, which when loaded also causes the existing Tk library on your system to be loaded. The second, ttk, is Python's binding to the newer themed widgets that were added to Tk in 8.5. yeah, from package import * doesn't actually import every name from a module. For example, by default, names starting with an underscore are not imported. Alternatively, if you have a variable named __all__ in your module, and it's a list of names, only those names in the list actually get imported when you do a from x import * Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
On 08/26/2010 10:29 PM, Hugo Arts wrote: On Thu, Aug 26, 2010 at 9:16 PM, Bill Allen walle...@gmail.com wrote: I did try that and of course it gave an error because it was necessary. I just did not know why. However, I later found an explanation on the web. Here it is: from tkinter import * from tkinter import ttk These two lines tell Python that our program needs two modules. The first, tkinter, is the standard binding to Tk, which when loaded also causes the existing Tk library on your system to be loaded. The second, ttk, is Python's binding to the newer themed widgets that were added to Tk in 8.5. yeah, from package import * doesn't actually import every name from a module. For example, by default, names starting with an underscore are not imported. Alternatively, if you have a variable named __all__ in your module, and it's a list of names, only those names in the list actually get imported when you do a from x import * Hugo Why would the person who wrote this package choose to do it this way, though? If it was something that people would use and not just an internal name, why hide it this way? Greg ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
On Thu, Aug 26, 2010 at 11:18 PM, Greg Bair gregb...@gmail.com wrote: On 08/26/2010 10:29 PM, Hugo Arts wrote: On Thu, Aug 26, 2010 at 9:16 PM, Bill Allen walle...@gmail.com wrote: I did try that and of course it gave an error because it was necessary. I just did not know why. However, I later found an explanation on the web. Here it is: from tkinter import * from tkinter import ttk These two lines tell Python that our program needs two modules. The first, tkinter, is the standard binding to Tk, which when loaded also causes the existing Tk library on your system to be loaded. The second, ttk, is Python's binding to the newer themed widgets that were added to Tk in 8.5. yeah, from package import * doesn't actually import every name from a module. For example, by default, names starting with an underscore are not imported. Alternatively, if you have a variable named __all__ in your module, and it's a list of names, only those names in the list actually get imported when you do a from x import * Hugo Why would the person who wrote this package choose to do it this way, though? If it was something that people would use and not just an internal name, why hide it this way? http://docs.python.org/tutorial/modules.html#importing-from-a-package It's a practical thing, mostly for packages. When you import * from a package 'x', how does python know all the modules present in the package? You would hope that it somehow goes into the filesystem and figures out all the files inside the 'x' directory, but this could take a long time and have unwanted side-effects. So, you have to specify the __all__ variable inside your __init__.py file, and that way import * will know what to do. Alternatively, you can just import all the modules inside __init__.py, since any names that are inside __init__.py will also be loaded. Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor