RPGObjects.com
http://www.rpgobjects.com/forum/

'Standard' duplicate imports
http://www.rpgobjects.com/forum/viewtopic.php?f=4&t=142
Page 1 of 1

Author:  prof.ebral [ Tue Feb 09, 2010 11:43 pm ]
Post subject:  'Standard' duplicate imports

I had to point this out hoping to get some feedback/. Below is the source of the start_client.py for Standard. My question is .. Why import all of os, end then import os.path? You don't even use the import 'path', you use os.path.

Another question, why import 100% of os when you are using chdir, and path? Same thing with sys, why import all of it when all you use is argv?

The duplicate made me curious.

Code:
#!/usr/bin/env python

import sys
import os, os.path
import runpy

runpy.run_module('updater.gui', run_name='__main__', alter_sys=True)

import pyver
pyver.checkPyVersion()

from orpg.orpg_wx import *
import orpg.main
from orpg.tools.orpg_log import logger

if __name__ == "__main__":
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
if WXLOADED:
mainapp = orpg.main.orpgApp(0)
mainapp.MainLoop()
else:
logger.exception("You really really need wx!")

Author:  Digitalxero [ Mon Mar 22, 2010 7:55 pm ]
Post subject:  Re: 'Standard' duplicate imports

the import os, os.path is a hold over from python 2.3 where you could not just do
Code:
import os
os.path.blabla


As for why importing all of sys and os, because it is faster and less error prone the importing each individual item from the module when using builtin modules where you are likely to use many different methods / submodules like sys, os, time, re, etc. Also it is very good for name-spacing so I dont accidentally overwrite a method / attribute by doing

Code:
from os.path import dirname
...
dirname = "/usr/local/bin/bla"
...
dirname(something) #Exception because I messed up the os.path.dirname method

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/