namespace collisions

Discussion in 'Python' started by Jeremy Bowers, Feb 17, 2005.

  1. On Thu, 17 Feb 2005 18:20:36 +0000, Will McGugan wrote:
    > I'm accumulating a number of small functions, which I have sensibly put
    > in a single file called 'util.py'. But it occurs to me that with such a
    > generic name it could cause problems with other modules not written by
    > myself. Whats the best way of handling this? If I put it in a common
    > location in my Python path, should I call it willsutil.py?


    Yes, something like that would be best. Two modules with the same name can
    cause problems because once one is imported, the other will never be;
    'import' just returns the first out of the cache.

    Looking on my system, I see 6 "utils.py" files; one in the xmlproc parser,
    two in twisted, one in docutils, one in a program called "sgmltools", and
    one in the xblproc parser in Jython, which I didn't even know I had
    installed(!). While I don't think you should have a problem because those
    are submodules (that is, you can't 'import utils', you have to 'import
    docutils.utils' and that doesn't create a 'utils' module), I'd say that
    in general, that's a scary enough name that you are likely to encounter
    trouble later.
    Jeremy Bowers, Feb 17, 2005
    #1
    1. Advertising

  2. Jeremy Bowers

    Will McGugan Guest

    Hi,

    I'm accumulating a number of small functions, which I have sensibly put
    in a single file called 'util.py'. But it occurs to me that with such a
    generic name it could cause problems with other modules not written by
    myself. Whats the best way of handling this? If I put it in a common
    location in my Python path, should I call it willsutil.py?

    TIA,

    Will McGugan
    Will McGugan, Feb 17, 2005
    #2
    1. Advertising

  3. Jeremy Bowers

    John Lenton Guest

    On Thu, Feb 17, 2005 at 06:20:36PM +0000, Will McGugan wrote:
    > Hi,
    >
    > I'm accumulating a number of small functions, which I have sensibly put
    > in a single file called 'util.py'. But it occurs to me that with such a
    > generic name it could cause problems with other modules not written by
    > myself. Whats the best way of handling this? If I put it in a common
    > location in my Python path, should I call it willsutil.py?


    local.util

    is probably a convention worth starting :)

    or you could go with

    WilMcGugan.util

    but ThatGetsOldFast.


    --
    John Lenton () -- Random fortune:
    All my friends are getting married,
    Yes, they're all growing old,
    They're all staying home on the weekend,
    They're all doing what they're told.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.0 (GNU/Linux)

    iD8DBQFCFONYgPqu395ykGsRAvqbAJ4wiEJWQOaxBE0dLPvTgEK6vATJ6wCeOjSL
    yBk5ZhTXd3RV4KP+8WghmV8=
    =6DVB
    -----END PGP SIGNATURE-----
    John Lenton, Feb 17, 2005
    #3
  4. Jeremy Bowers

    wes weston Guest

    Will McGugan wrote:
    > Hi,
    >
    > I'm accumulating a number of small functions, which I have sensibly put
    > in a single file called 'util.py'. But it occurs to me that with such a
    > generic name it could cause problems with other modules not written by
    > myself. Whats the best way of handling this? If I put it in a common
    > location in my Python path, should I call it willsutil.py?
    >
    > TIA,
    >
    > Will McGugan

    Will,
    See http://www.boost.org/libs/python/doc/tutorial/doc/html/python/techniques.html

    'about page 30 of google search. It gives an example
    that should help.
    wes
    wes weston, Feb 17, 2005
    #4
  5. Will McGugan wrote:
    > I'm accumulating a number of small functions, which I have sensibly put
    > in a single file called 'util.py'. But it occurs to me that with such a
    > generic name it could cause problems with other modules not written by
    > myself. Whats the best way of handling this? If I put it in a common
    > location in my Python path, should I call it willsutil.py?


    I find that it's beneficial in the long run to create a package for your
    project. This will insure you against name collisions in general, should
    you later need to combine projects::

    import myproject.util
    myproject.util.myhandyfunction()
    etc.

    The potential downside is that if you really need to reuse util.py in
    multiple projects, you'll have to copy the file to each package, or
    create a central package that the other projects' packages link to. But
    it's been my experience that the type of stuff that goes in "util"
    modules is pretty miscellaneous in nature, and not as reusable as it
    seems. Not worth creating additional dependencies, anyway.

    There's really no one right answer to your question, but I've been
    (mildly) bitten by naming collisions, and as a result I use packages for
    every project now.

    Dave
    Dave Benjamin, Feb 17, 2005
    #5
  6. Jeremy Bowers

    Guest

    John Lenton wrote:
    > On Thu, Feb 17, 2005 at 06:20:36PM +0000, Will McGugan wrote:
    > > Hi,
    > >
    > > I'm accumulating a number of small functions, which I have sensibly

    put
    > > in a single file called 'util.py'. But it occurs to me that with

    such a
    > > generic name it could cause problems with other modules not written

    by
    > > myself. Whats the best way of handling this? If I put it in a

    common
    > > location in my Python path, should I call it willsutil.py?

    >
    > local.util
    >
    > is probably a convention worth starting :)
    >
    > or you could go with
    >
    > WilMcGugan.util
    >
    > but ThatGetsOldFast.
    >
    >
    > --
    > John Lenton () -- Random fortune:
    > All my friends are getting married,
    > Yes, they're all growing old,
    > They're all staying home on the weekend,
    > They're all doing what they're told.


    I call modules like x_utils.py, where prefix x is assigned to a
    particular pakage. In this case import statements look like:

    import Company.repotrtools.e_excel as e_excel

    or

    from Company.repotrtools.e_excel import e_writer, e_reader

    generally this are classes, not functions.

    Newer had namespace collisions (yet :)
    , Feb 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howie
    Replies:
    4
    Views:
    335
    Howie
    Aug 21, 2003
  2. Vam
    Replies:
    2
    Views:
    290
    Andrey Tarasevich
    Feb 14, 2005
  3. Richard Bos
    Replies:
    3
    Views:
    356
    CBFalconer
    Feb 6, 2004
  4. Peter Ammon

    Avoiding name collisions in macros

    Peter Ammon, May 21, 2004, in forum: C Programming
    Replies:
    6
    Views:
    576
    Chris Torek
    May 23, 2004
  5. Robert Brewer
    Replies:
    0
    Views:
    238
    Robert Brewer
    Oct 6, 2004
Loading...

Share This Page