[Tutor] Re: Advice needed on first project

Andrei project5 at redrival.net
Thu Apr 15 13:33:41 EDT 2004


Adam wrote on Thursday 15 April 2004 18:49:
<snip>
>> Try replacing the line:
>> create_new_article.newarticle()
>> with
>> article_items  = create_new_article.newarticle()
> I did try this earlier, but must have had a syntax problem, or something
> similar - it seemed to me to be the most obvious thing to do. Am I right
> in thinking that the passed article_items and the menu article_items are
> different references - or do they point to the same object? What I'm
> trying to say is, does it produce a copy of that object, or is it
> another pointer to that same object ?

Not a pointer, a reference. Assignments always bind an existing object to a
new name. You can avoid that and force Python to create a new object by
using the copy module (copy.copy and copy.deepcopy).

>> You might also consider using triple quoted milti line strings for
>> your welcome message.
> 
> What benefit does this offer? I thought that the quotes pretty much
> acted in the same ways, unless you used 'special chars', like " and '
> inside them. Does this have something to do with the newline characters?

Triple-quoted strings can have this form:
"""First line
Second line""",
which makes them a better way of specifying long strings. So it does have to
do with the newline characters in long strings :). It would also remove the
need to make a function especially for the purpose of showing the welcome
string, since it could simply be a variable which gets printed, "print
welcome".

Another neat trick is to use a docstring at the top of your module and use
that for welcome too:

"""My doc""" # doc string for module
print __doc__ # print docstring at top of module

Other suggestions about the code:

- don't use tabs. The preferred way of writing Python code is by using 4
spaces per indentation level. This is what you do in create_new_articles,
but you use tabs in menu. If you now try to copy-paste code from the one to
the other in order to avoid the mini-modules, you'll get some errors I
think.
The best way to not worry about this is to set up your editor in such a way
that pressing TAB will insert 4 spaces and that auto-indentation uses 4
spaces.

- raw_input takes a parameter:
    >>> raw_input("Your name: ")
    Your name: Adam
    'Adam'
Use this in order to avoid all the print statements in create_new_article.
This will almost halve the amount of code in that function.

- always assume your users are stupid - I do that even if I only code for
myself :). What happens if your user answers "n" or 'new' in the menu or
accidentally types a space before or after the 'n'? He's told the choice is
invalid. Case sensitive menus are not a very good idea. That's why it makes
sense to clean up the response a bit:

  response = response.strip().lower() # remove spaces and lower-case it

In order to account for cases when the user answers 'new', you could also:

  response = response[:1] # only look at the first char

- there's no way to exit from the menu, nor instructions on how to terminate
the program by other means (Ctrl+Z).

-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.




More information about the Tutor mailing list