limit module import to only certain symbols?

Discussion in 'Python' started by Kevin Howe, Jun 26, 2003.

  1. Kevin Howe

    Kevin Howe Guest

    Say I have the following module named "mymodule"

    import string
    var1 = 'a'
    var2 = 'b'

    And I want to import its symbols into another script:

    from mymodule import *

    This will import 3 symbols (string, var1, var2)

    Is it possible to limit it so that import * will only import specified
    items? So that:

    from mymodule import *

    Would import only var1 and var2?

    - Kevin
     
    Kevin Howe, Jun 26, 2003
    #1
    1. Advertising

  2. "Kevin Howe" <> writes:

    > Say I have the following module named "mymodule"
    >
    > import string
    > var1 = 'a'
    > var2 = 'b'
    >
    > And I want to import its symbols into another script:
    >
    > from mymodule import *
    >
    > This will import 3 symbols (string, var1, var2)
    >
    > Is it possible to limit it so that import * will only import specified
    > items? So that:
    >
    > from mymodule import *
    >
    > Would import only var1 and var2?


    You have to write

    __all__ = ["var1", "var2"]

    in your mymodule.

    Thomas
     
    Thomas Heller, Jun 26, 2003
    #2
    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.

Share This Page