LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2007, 02:36 PM   #1
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Python / using modules in class methods


Ok.. check out the following some-what-pseudocode:

Code:
class introuble:
    def do_something(self):
        import some_module
        some_module.some_function()

    def do_something_else(self):
        import some_module
        some_module.some_function_2()
The thing is.. I'd like *not* to import "import some_module" within each method.. but rather import it only once. Is this possible?
 
Old 01-04-2007, 02:42 PM   #2
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
Figured it out just a couple of seconds after this post

Code:
class introuble:
    import some_module
    def do_something(self):
        self.some_module.some_function()
 
Old 01-04-2007, 07:24 PM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Exclamation

A module only gets imported once regardless of how many times "import some_module" gets executed. So, except for stylistic reasons, there's really no need to rewrite it.
 
Old 01-16-2007, 10:00 AM   #4
billiejoex
Member
 
Registered: Oct 2005
Posts: 31

Rep: Reputation: 15
In python exists "namespaces". If you import a certain module inside a class/function it will only be available in the class/function namespace:
Code:
>>> def fun():
...     import os
...
>>> fun()
>>> os
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'os' is not defined
>>>
Otherwise, if you import module in the 'global' namespace it will available for any class/module of your script:
Code:
>>> import os
>>> def fun():
...     os
...
>>> os
<module 'os' from 'C:\Python24\lib\os.pyc'>
>>> fun()
>>>

Last edited by billiejoex; 01-16-2007 at 11:09 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Making Card Class (In Python) br00tal Programming 1 03-24-2006 02:13 AM
Class Arrays in Python rootyard Programming 1 02-13-2004 10:58 PM
Overloading methods in Python? xaoc Programming 0 10-28-2003 10:03 AM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM
Python modules in Superkaramba wakeupbomb Linux - Software 12 08-03-2003 05:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration