|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
|||
|
|||
|
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 ![]() |
|
#3
|
||||
|
||||
|
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 >>> |
|
#4
|
|||||
|
|||||
|
Good Boki,
But then the practice of using from module import * has been frowned upon.... Quote:
and read this one.............. Quote:
more on http://diveintopython.org/object_or...ng_modules.html A final word........... Quote:
Rgds, Subha ![]() |
|
#5
|
||||
|
||||
|
Quote:
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. |
|
#6
|
||||
|
||||
|
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... ![]() |
|
#7
|
||||
|
||||
|
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 ![]() |
|
#8
|
||||
|
||||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Thinking Modular |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|