Calling a function from module question.

Joe Francia gmane-schpam at joefrancia.com
Tue Feb 15 17:32:48 EST 2005


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




More information about the Python-list mailing list