Re: [Tutor] Advice needed on first project

Magnus Lycka magnus at thinkware.se
Thu Apr 15 10:56:14 EDT 2004


Adam wrote:
> I've started coding some python as my first attempt at learning it - I 
> kind of learn things by doing them.

Welcome to the world of Python! I hope you will enjoy it.
 
> You can see the code at: http://www.monkeez.org/python/mag/menu.txt and 
> http://www.monkeez.org/python/mag/create_new_article.txt
> 
> How do I return the article_items so that menu.py has access to it? 

You return it alright. That's not the problem...

> Does 
> "global article_items" only affect the create_new_article module, or 

Global means global within a module (i.e. file) in Python. You 
shouldn't use global variables in cases like this anyway.

> will it apply even further. In my case, it only seems to apply to that 
> function.

Your problem is that, while the newarticle() function returns a value,
you don't receive it, you just throw it away! You must assign the value
you receive to a variable in the calling scope.

Try replacing the line:

create_new_article.newarticle()

with

article_items  = create_new_article.newarticle()

Then you obviously need some more code that actually does something
with the article items.

> I'd also appreciate some feedback on the code design. Does the code 
> reflect the best design, or would there be a better way of doing this?

No. There is always a better way of doing things. Is it good enough?
Well, since it doesn't actually work yet, and seems far from complete,
I guess it's a bit early to say...

One detail is that you shouldn't make lines longer that 70-80 characters
(just like with normal text) as it gets difficult to read.

It's also not very pythonic to make such small modules. I this case
I see no reason to make more than one file. Split it if it really needs
to be split, i.e. if it gets big, has several maintainers etc. With the
typical "if __name__ == '__main__':" trick, you can still use the code
as a module even if it also works as a stand alone program.

I also see two cases where it might be more pythonic to use a loop
and a more data driven approach, 

You might also consider using triple quoted milti line strings for
your welcome message.

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list