1

I use functools and itertools in pretty much every module. I find them so essential that I'm annoyed that I have to import their functions in every module I write. I'd like to create a module, let's call it mytools that imports all of the functions from functools and itertools and makes their functions directly available to any module that imports mytools.

I'm specifically interested in manipulating the import system in this way, so please avoid sidestepping this approach in solutions just for the sake of making functools and itertools easily available.

5
  • 1
    You should just automate your python file headers using a template in your IDE. Commented May 10, 2018 at 22:02
  • 1
    ... have you tried creating a module mytools that itself imports what you want? What exactly is your question here, what difficulty are you encountering? Commented May 10, 2018 at 22:30
  • @juanpa.arrivillaga Why do you start your comment with a "..."? The only interpretation I can guess is that you want to express that the question is stupid or misguided. Is that what you wanted to express? What exactly is the point of your comment? I think I made my question perfectly clear. If you think that it doesn't warrant submission on this site, save your unnecessary comments and vote to close. Commented May 10, 2018 at 23:25
  • 2
    Because your question seems to answer itself, and I don't understand what difficulty you are encountering. I am asking for you to elaborate. The '...' expresses my lack of understanding you. Commented May 10, 2018 at 23:49
  • It's because the question is too broad. I'm voting to close. Commented May 10, 2018 at 23:50

2 Answers 2

2

Create a directory mytools and add it's parent directory to your python path. Inside that directory create a file __init__.py with

from functools import *
from itertools import *

Then from other files you can do

import mytools
print(mytools.partial)
# <class 'functools.partial'>
Sign up to request clarification or add additional context in comments.

6 Comments

There seems to be no reason to use a package here. Also, adding the mytools directory itself to the path won't make mytools importable, and manually messing with the path is usually a bad way to go in general.
What if both modules have a function/object using the same name but with different signatures? That is why from module import * is frowned upon.
@user2357112 Then where would you place it to make it discoverable?
You could also do from mytools import *. Then, you just populate the mytools namespace (by doing imports etc) how you want, and your new module starts with the same namespce.
@PatrickHaugh: In the same directory as the other modules, or make a setup.py and pip install the module (probably in a virtualenv), or maybe there's some sort of IDE-specific thing to do if you use an IDE (I don't).
|
0

You could create your own class that imports and defines them as being self names:

import itertools
import functools


class MyTools(object):
    def __init__(self):
        self.itertools = itertools
        self.functools = functools

You would then just have to import the file containing this class:

from mytools.py import MyTools
mytools = MyTools()

print(mytools.functools.partial)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.