Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 23rd, 2004, 06:37 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 95 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 2 m 36 sec
Reputation Power: 5
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?!?!?

Reply With Quote
  #2  
Old November 23rd, 2004, 11:48 PM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
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

Reply With Quote
  #3  
Old November 24th, 2004, 12:17 AM
Boki's Avatar
Boki Boki is offline
A wanna-be guru of some sort
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Either online or offline
Posts: 624 Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 7 m 13 sec
Reputation Power: 14
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
>>>

Reply With Quote
  #4  
Old November 24th, 2004, 04:41 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Good Boki,
But then the practice of using from module import * has been frowned upon....

Quote:
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..............

Quote:
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_or...ng_modules.html


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

Quote:
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

Reply With Quote
  #5  
Old November 24th, 2004, 06:12 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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


Reply With Quote
  #6  
Old November 24th, 2004, 06:16 PM
Boki's Avatar
Boki Boki is offline
A wanna-be guru of some sort
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Either online or offline
Posts: 624 Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 7 m 13 sec
Reputation Power: 14
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...

Reply With Quote
  #7  
Old November 24th, 2004, 06:54 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 95 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 2 m 36 sec
Reputation Power: 5
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

Reply With Quote
  #8  
Old November 25th, 2004, 04:02 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Thinking Modular


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT