ten small Python programs

Steve Howell showell30 at yahoo.com
Sat May 26 16:05:02 EDT 2007


--- Steven Bethard <steven.bethard at gmail.com> wrote:
> Very cool! Do you mind putting this up on the Wiki
> somewhere so that we 
> can link to it more easily? Maybe something like:
> 
>      http://wiki.python.org/moin/SimplePrograms
>

Done.
 
> <nitpick>
> Though the code should probably follow PEP 8
> guidelines, e.g. 
> under_scores instead of camelCase for object and
> method names:
> 
>      http://www.python.org/dev/peps/pep-0008/
> </nitpick>
> 

I think I fixed them, please see Wiki to verify.


> >     class ShoppingCart:
> >         def __init__(self): self.items = []
> >         def buy(self, item):
> self.items.append(item)
> >         def boughtItems(self): return self.items
> >     myCart = ShoppingCart()
> >     myCart.buy('apple')
> >     myCart.buy('banana')
> >     print myCart.boughtItems()
> 
> I think boughtItems() is probably not a good example
> of Python code 
> since in this case, you should probably just write
> ``my_cart.items``. 
> Maybe it should define ``__len__`` instead? Or maybe
> something like::
> 
>      def select_items(self, prefix):
>          return [item for item in self.items if
> item.startswith(prefix)]
> 

I think the problem here is that it's hard to write a
useful class in less than 10 lines of code.  Can
somebody else give it a try?

Although I didn't call it out in the email, I tried to
make each program progressively one line longer, so if
somebody wants to write, say, an 11-line class
example, then I will try fill in the gap with another
8-liner.

Did I miss any basic concepts in the first 10
programs?  Maybe an 8-liner could demonstrate command
line arguments?






       
____________________________________________________________________________________Yahoo! oneSearch: Finally, mobile search 
that gives answers, not web links. 
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC



More information about the Python-list mailing list