Calling a function from module question.

Discussion in 'Python' started by Sean, Feb 15, 2005.

  1. Sean

    Sean Guest

    Is there any way I could have the following work?

    First I would have a module define a function to do
    something like print some data.

    ----- module_name.py -----

    [snip]

    def print_this(data):
    print "This is the data: %s" %data

    [/snip]

    -----------------------------


    Then I would have a script that uses the
    print_this function defined in the module
    without using the module name in the call.

    ----- test_file.py -----

    [snip]

    import module_name.py
    print_this("lots of data")

    [/snip]

    ----------------------

    Now, I know I can call the function using
    module_name.print_this("lots of data")
    but can using the module name at the beginning
    be avoided?

    If not, why? I am sure there is a good pythonic
    explanation.

    Thanks
     
    Sean, Feb 15, 2005
    #1
    1. Advertising

  2. Sean wrote:

    > Then I would have a script that uses the
    > print_this function defined in the module
    > without using the module name in the call.




    from module_name import print_this

    or, even:

    from module_name import print_this as other_nice_name

    --Irmen
     
    Irmen de Jong, Feb 15, 2005
    #2
    1. Advertising

  3. Sean

    Sean Guest

    > Sean wrote:
    >
    >> Then I would have a script that uses the
    >> print_this function defined in the module
    >> without using the module name in the call.

    >
    >
    >
    > from module_name import print_this
    >
    > or, even:
    >
    > from module_name import print_this as other_nice_name
    >


    So what if I have a whole bunch of functions - say 25 of them.
    Is there a way to do this without naming each function?
     
    Sean, Feb 15, 2005
    #3
  4. Sean wrote:
    >>Sean wrote:
    >>
    >>>Then I would have a script that uses the
    >>>print_this function defined in the module
    >>>without using the module name in the call.

    >>
    >>from module_name import print_this
    >>
    >>or, even:
    >>
    >>from module_name import print_this as other_nice_name

    >
    > So what if I have a whole bunch of functions - say 25 of them.
    > Is there a way to do this without naming each function?


    Yes [1], but it's basically deprecated and you shouldn't use it.
    Consider refactoring your code.

    Steve

    [1] http://docs.python.org/ref/import.html
     
    Steven Bethard, Feb 15, 2005
    #4
  5. Sean

    Sean Guest

    >>>from module_name import print_this
    >>>
    >>>or, even:
    >>>
    >>>from module_name import print_this as other_nice_name

    >>
    >> So what if I have a whole bunch of functions - say 25 of them.
    >> Is there a way to do this without naming each function?

    >
    > Yes [1], but it's basically deprecated and you shouldn't use it. Consider
    > refactoring your code.
    >


    Refactoring my code? Sorry, I am not sure what you mean here.

    How would one refactor the example in my original post?
     
    Sean, Feb 15, 2005
    #5
  6. Sean wrote:
    >>>>from module_name import print_this
    >>>
    >>>>or, even:
    >>>>
    >>>
    >>>>from module_name import print_this as other_nice_name
    >>>
    >>>So what if I have a whole bunch of functions - say 25 of them.
    >>>Is there a way to do this without naming each function?

    >>
    >>Yes [1], but it's basically deprecated and you shouldn't use it. Consider
    >>refactoring your code.

    >
    > Refactoring my code? Sorry, I am not sure what you mean here.
    >
    > How would one refactor the example in my original post?


    The original post only had one name to import, not 25, so refactoring
    isn't really necessary. ;) What are the 25 functions you want to
    import? Perhaps you can group them together in classes? Or maybe a
    couple of (sub-)modules is the way to go...

    STeVe
     
    Steven Bethard, Feb 15, 2005
    #6
  7. Sean

    Terry Reedy Guest

    "Sean" <> wrote in message
    news:UTsQd.32669$6u.27954@fed1read02...
    > import module_name.py


    leave off the .py

    Irmen answered your main question.

    Terry J. Reedy
     
    Terry Reedy, Feb 15, 2005
    #7
  8. Sean

    Jeff Shannon Guest

    Sean wrote:

    >>>So what if I have a whole bunch of functions - say 25 of them.
    >>>Is there a way to do this without naming each function?

    >>
    >>Yes [1], but it's basically deprecated and you shouldn't use it. Consider
    >>refactoring your code.

    >
    > Refactoring my code? Sorry, I am not sure what you mean here.


    'Refactoring' is just a fancy way of saying 'reorganizing'. What it
    means in this case is to look at the reason that you have 25 functions
    in this other module whose name you don't want to type. Perhaps
    reassembling those functions into a class or two will let you have
    fewer names to import, or perhaps there's no compelling reason for
    them to be in a different module to begin with. (Or, more likely, you
    should just not worry about using the module name. It's really better
    to keep track of where all of your names come from, and fully
    qualified names do that nicely. What do you see as the harm of using it?)

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Feb 15, 2005
    #8
  9. Sean

    Joe Francia Guest

    Sean wrote:
    >>Sean wrote:
    >>
    >>
    >>>Then I would have a script that uses the
    >>>print_this function defined in the module
    >>>without using the module name in the call.

    >>
    >>
    >>
    >>from module_name import print_this
    >>
    >>or, even:
    >>
    >>from module_name import print_this as other_nice_name
    >>

    >
    >
    > So what if I have a whole bunch of functions - say 25 of them.
    > Is there a way to do this without naming each function?
    >
    >


    You do that like so: "from module import *". But you should avoid that,
    as stated in the Python help:

    Note that in general the practice of importing * from a module or
    package is frowned upon, since it often causes poorly readable code.
    However, it is okay to use it to save typing in interactive sessions,
    and certain modules are designed to export only names that follow
    certain patterns.

    The "certain patterns" usually occur in huge packages, such as in the
    various GUI toolkits. E.g., all of the exported PyQt classes are
    prefaced with Q (QButtonGroup, QTabWidget), so doing "from qt import *"
    is fairly safe.

    You can also import a module like so: "import module as m" to save on
    some typing, if that is your concern. But namespaces are a feature of
    Python, not a limitation, so the Python way is to use them for clearer
    code. With a large number of functions like that, it sounds more like
    you should be inheriting from a class anyway, which I think is what
    Steven Bethard meant when he suggested refactoring.

    For more information on the Python way, go to the Python interpreter and
    type "import this" ;>)

    --
    Soraia: http://www.soraia.com
     
    Joe Francia, Feb 15, 2005
    #9
  10. Sean

    JRCondon Guest

    Sean, if you are asking what I think you are asking (I don't think name
    hiding is the issue), you can use

    from module_name import *

    and you will end up with all of the functions at session scope. You can
    use the 'as' to alias the function names if you wish

    from module_name import fn1 as myfn1, fn2 as myfn2

    but, um, that gets confusing.
     
    JRCondon, Feb 17, 2005
    #10
    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. Andreas Lagemann
    Replies:
    8
    Views:
    486
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    445
  3. Marc Shapiro
    Replies:
    2
    Views:
    651
    Marc Shapiro
    Dec 19, 2003
  4. Gopal
    Replies:
    2
    Views:
    290
    Mikael Olofsson
    Sep 27, 2005
  5. Maric Michaud
    Replies:
    0
    Views:
    7,194
    Maric Michaud
    Jun 24, 2006
Loading...

Share This Page