Message from discussion
ten small Python programs
Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Sat, 26 May 2007 14:38:44 -0500
Date: Sat, 26 May 2007 13:38:44 -0600
From: Steven Bethard <steven.beth...@gmail.com>
User-Agent: Thunderbird 2.0.0.0 (Windows/20070326)
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: Re: ten small Python programs
References: <mailman.8216.1180205410.32031.python-list@python.org>
In-Reply-To: <mailman.8216.1180205410.32031.python-list@python.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <mcidnYViZIlYEcXbnZ2dnUVZ_uKknZ2d@comcast.com>
Lines: 44
NNTP-Posting-Host: 67.166.43.236
X-Trace: sv3-mn3xfqUVh6MAvRrMQ6gnMGpbYcrWf6Qelc97kPQh5BQdZhCXQqUjXh92kxMQDX6tYDcY+qcZO9ddmSj!VcS/gWTn/N/nKGa4ToWHftOwxjHsb+JZdG8GoO8wWFUKxkBBVNLvg45vk8AdoCmBZ/P/PB8GNQJF!B9WmdBgKGxOVJFWDIxGwL34uMp8yLA==
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: d...@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.34
Steve Howell wrote:
> I've always thought that the best way to introduce new
> programmers to Python is to show them small code
> examples.
>
> When you go to the tutorial, though, you have to wade
> through quite a bit of English before seeing any
> Python examples.
>
> Below is my attempt at generating ten fairly simple,
> representative Python programs that expose new users
> to most basic concepts, as well as the overall syntax.
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
<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>
> 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)]
STeVe