4

I am writing a program in python. I am using about a dozen modules. Currently I have placed them in the functions themselves. My reasoning is that since a user is not going to use all the functions in a session, many modules don't have to be imported uselessly until a function requires them. But all the code I have seen till now have their imports at the beginning. What are their reasons and advantages?

Also what is the best way to import ? I have seen several types like:

  1. from tkinter import * and then Label(), Tk()
  2. import tkinter and then tkinter.Label(), tkinter.Tk()
  3. import tkinter as tk and then tk.Label()?

What's the best way? I see first one is shortest. Is it also the best?

Suresh Subedi
  • 660
  • 2
  • 10
  • 25
  • @martijn-pieters that answer is very good for the first part of my question. i consider first part of the question answered. its second part remains unanswered though. shall i remove this post and create a new thread or edit post to contain second part only? – Suresh Subedi Jan 12 '14 at 03:22

1 Answers1

2

I will try to answer the second part of your question.

  1. from tkinter import * and then Label(),Tk() This is considerd bad practice, because it mixes the namespaces and can confuse future readers of your code. For example, many modules have a sqrt function (the standard library math, numpy, scipy). If you use import all (*) for several of them, the last one will win. Sometimes a module has functions which will overwrite standard python functions, and the reader cannot know where this specific function comes from. Even if you only use import-all on one module, the reader has to differentiate between standard python functions and the package functions, e.g. is getint from tkinter? why did he use NONE instead of None etc.. This should only be used for interactive work on the interpreter.

  2. import tkinter and then tkinter.Label(), tkinter.Tk() This is the most explicit (and do remember that explicit is better than implicit), but it is a bit long.

  3. import tkinter as tk and then tk.Label()? This one is a good compromise between 1 and 2. Short but explicit.

So you have to decide between 2 and 3. If you use the package often, and it has a clear short name, (such as tkinter -> tk, numpy -> np) I would use the short name.

jarondl
  • 1,593
  • 4
  • 18
  • 27
  • I believe I somewhere (effbot.org and webpage linking there) read that tkinter was designed to be imported as `from tkinter import *` without problems but since I can't find that post and since I read many other posts that are against such import, I accept your answer. – Suresh Subedi Jan 13 '14 at 14:14
  • @love_in_dreams: there's no way to design any python package to be imported this way without causing problems. The very nature of importing things into a global scope will *always* have the potential for problems. Tkinter is particularly susceptible since people often use tkinter and ttk together, and they both have classes named Button, Label, etc. – Bryan Oakley Jan 16 '14 at 22:57
  • ok thanks to you both. we are all too lazy i guess because i don't see many people/examples using the correct way. Using shortname indeed seems like the best way, for production code anyway. – Suresh Subedi Jan 20 '14 at 02:20