It is not clear if you're more embarassed by the length of identification expressions to write, or by the trail of remaining identifiers in a namespace after their use.
For the first, the technique of defining an alias for a long prefix, as described by Raymond Hettinger, is THE ONE to employ.
For the second, I'm surprised that nobody resorted to the importation of a module in which the instructions and lines you consider heavy and littering are consigned.
By the way, if you access to functions by os.path.abspath
and os.path.dirnames
, it is incorrect to say that the functions (I suppose you mean their names) litter the namespace. Since they are belonging to the module os or os.path (it depends which one has been imported) , there are only the name of the module 'os' or 'os.path' in the namespace, and the module object in the memory, but not the functions's names directly in the namespace.
So, one can create a script of name "heavy_code.py" :
def doing(x):
from os.path import abspath as a,dirname as d
## Execute all the desired processes and creations
def fufu(s,t):
return s+t
dedex = d[x]
#.........
#...........
#........
#............
#..........
## Return to the calling scope all that is needed there
return (dedex,fufu)
And in the main module, taking acount of gnibbler's answer:
one_path = 'I:/all/magala/zeru/kiol.py'
try:
from pp.bududu import doing
w,ff = doing(one_path)
finally:
del doing
.
To see how it works:
one_path = 'I:/all/magala/zeru/kiol.py'
try:
from pp.bududu.heavy_code import doing
print "in try : "
print dir()
print "executing doing()"
w,ff = doing(one_path)
print dir()
finally:
del doing
print "\nafter finally : "
print dir()
print '\nw ==',w
print 'ff(10,12) ==',ff(10,12)
produces as a result:
in try :
['__builtins__', '__doc__', '__name__', '__package__', 'doing', 'one_path']
executing doing()
['__builtins__', '__doc__', '__name__', '__package__', 'doing', 'ff', 'one_path', 'w']
after finally :
['__builtins__', '__doc__', '__name__', '__package__', 'ff', 'one_path', 'w']
w == I:/all/magala/zeru
ff(10,12) == 22
After the execution of the snippet, the function doing() doesn't exist anymore in the main module, but objects created by the execution of doing() now lay in it without a clutter of names in the main module's namespace. Also, all the identifiers needed inside the function doing() are local to it .
The creation of all the desired and needed objects can be delegated to the module heavy_code, whatever how many they are, while importing and executing the function doing() takes only two lines in the main module, and function doing() in heavy_code plus its calling line can easily be modified.
Isn't it what are the module designed for ?