#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11

    Thinking Modular


    Greets

    Well since of Python's strong points,is the ability to make modules to be imported into other programs at a later date,I've decided to try my hand at it.But being a non-programmer,I am having trouble implenting it.for example,lets say have some lists:

    Code:
    lista=['some', 'words', 'here']
    listb=['other', 'different', 'stuff']
    now when I define those lists inside my program,I am able to reffer to them w/o trouble.But what if I need the lists to be defined outside my program,and then imported at start up?example:

    database.py:
    Code:
    lista=['some', 'words', 'here']
    listb=['other', 'different', 'stuff']
    and in my program I think I would do this:
    program.py:
    Code:
    import database
    
    if 'stuff' in lista:
        print 'yes,in lista'
    elif 'stuff' in listb:
        print 'yes in listb'
    else:
        print 'not in either list'
    But I get an error saying lista is not defined.I know it's importing the right file(or atleast trying to) because when I do a "print database" it returns the filepath to my database module.The examples I have found on modules,all deal with using functions in those modules.But my module doesn't have functions,its just a bunch of lists that I need to reffer to.

    Can anyone shed some light for me for,on how to use one of Pyhtons greatest strong points(making+using your own modules)?Or perhaps I need to use a dictionary?If so,can the dictionary be defined outside the main program,and them imported and used like I want?
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hi,
    A module is a file which can contain variables, objects or other functions...any combination of these. Now when you create a module like database.py, it contains only lists... you can very well use it in other modules...like ur program.py.... provided you refer to the correct object...in simple words...it has to be module.variable or in case of functions... module.function() The advantage of this approach is that, your lista declared inside database.py won't clash with other lista declared in your program.py! Also it is an indication that lista belongs to this( database.py) module.


    So your program.py has to be....

    Code:
    import database
    
    if 'stuff' in database.lista:
        print 'yes,in lista'
    elif 'stuff' in database.listb:
        print 'yes in listb'
    else:
        print 'not in either list'

    Rgds,
    Subha
  4. #3
  5. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    A way to avoid prefixing everything with a module name is to do this:
    Code:
      
    from database import *
     
    >>> from database import *
    >>> 'some' in lista
    True
    >>> 'other' in lista
    False
    >>>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Good Boki,
    But then the practice of using from module import * has been frowned upon....

    When should you use from module import?

    If you will be accessing attributes and methods often and don't want to type the module name over and over, use from module import.
    If you want to selectively import some attributes and methods but not others, use from module import.
    If the module contains attributes or functions with the same name as ones in your module, you must use import module to avoid name conflicts.

    and read this one..............

    Use from module import * sparingly, because it makes it difficult to determine where a particular function or attribute came from, and that makes debugging and refactoring more difficult.
    more on http://diveintopython.org/object_ori...g_modules.html


    A final word...........

    As an analogy, you can overclock your PC to make it run faster, so why isn't
    everyone doing it? Because it's likely to cause problems eventually, even if you
    can get away with it for a while. Using "from module import *" won't fry your hardware, but it can end up costing a *lot* of wasted time tracking down bugs.
    Rgds,
    Subha
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you will be accessing attributes and methods often and don't want to type the module name over and over, use from module import.
    Some middle ground if you'd like; use the import module as name form to import the module with a different [shorter] name, this can cut down on the amount you have to type by a lot in large projects .

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Oh, well... but it should not cause problems when all you have in the entire module are two variables and when all you wanna do is give a little example... BTW, I read that book a long time ago...
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    I feel so silly AGAIN,but what's new?

    Forgive my terminology(or lack of hehe),but I misunderstood how import works.I thought once I imported a module,any variables I had defined inside would be available to use in the program that imported it...Just like as if I had defined those variables in the program itself.

    Anyways,I guess I was over thinking it,but thx to NewPythoner for finding my simple mistake...Just had to tell Python what I was trying to reffer to properly.

    Code:
    >>> import database
    >>> print database.lista
    ['some', 'words', 'here']
    >>> print database.listb
    ['other', 'different', 'stuff']
    >>> 'stuff' in database.lista
    False
    >>> 'stuff' in database.listb
    True
    p.s. Even though it may seem I run and post here at the first sign of trouble,I'm not.As you can tell,I still don't have a firm grasp on the snake,and that explains why I get stuck on simple things.

    Thank you very much to all who contributed.Feel free to point and laugh all you want,I have a sense of humor
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by Boceifus
    p.s. Even though it may seem I run and post here at the first sign of trouble,I'm not.As you can tell,I still don't have a firm grasp on the snake,and that explains why I get stuck on simple things.
    Feel free to post any questions you might have, we don't mind B'. After all, it's what were here for: to help people with there Pythons .

    We all have to start learning somewhere!

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo