Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9581

Re: list(), tuple() should not place at "Built-in functions" in documentation

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'subject:" ': 0.03; 'plenty': 0.04; 'parameter': 0.05; 'c++,': 0.07; 'python': 0.08; 'exception.': 0.09; 'iterate': 0.09; 'throw': 0.09; 'exception': 0.12; 'am,': 0.13; 'received:209.85.214.174': 0.13; 'received:mail-iw0-f174.google.com': 0.13; 'wrote:': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pointers': 0.16; 'subject:() ': 0.16; '16,': 0.16; 'method.': 0.16; 'def': 0.16; 'subject:list': 0.16; 'subject:skip:d 10': 0.17; 'subject:not': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'code.': 0.22; 'breaks': 0.23; 'controlling': 0.23; 'code': 0.24; 'parameters': 0.25; 'function': 0.26; '(and': 0.27; 'raise': 0.28; 'objects': 0.28; 'received:209.85.214': 0.28; 'sat,': 0.28; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.28; 'object': 0.30; 'user.': 0.31; 'coding': 0.32; 'list': 0.32; 'too': 0.32; "i'll": 0.33; 'actually': 0.33; "i've": 0.33; 'to:addr:python-list': 0.34; 'instead': 0.34; 'there': 0.34; 'idea': 0.36; 'rules': 0.36; 'sequence': 0.37; 'anything': 0.37; 'some': 0.37; 'doing': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'user': 0.38; 'subject:: ': 0.38; 'something': 0.38; 'run': 0.39; 'should': 0.39; 'list,': 0.39; 'allows': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; 'your': 0.60; 'stop': 0.61; 'huge': 0.64; 'students,': 0.64; 'here.': 0.66; 'subjects': 0.67; 'care': 0.73; 'fans,': 0.84; 'me:': 0.84; 'subject:should': 0.84; 'subject:place': 0.91; 'enhancement': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=FEcSaOTzsMk+F56y/Ff/MJN/5YQAp1EBY2I4M33rU60=; b=Eda/pmxz1w8QKd0JdMXiOCqhd59po87xfDGddNOYSO8JkbvQw/Sdfojri/euHXuyKK RZpYvhYBZkt/VkshVAYcDDB7/3sQ240beExU4ZiCD5lm1v3IfEg84agK1V9YfmTJdAIJ TrfJbKksIV2k2KLFGNgAIAGpIcvqpVwLpgWYI=
MIME-Version 1.0
In-Reply-To <cf0e04cc-a065-4fc4-ab25-777adadafb81@glegroupsg2000goo.googlegroups.com>
References <87ei1sb3x9.fsf@benfinney.id.au> <cf0e04cc-a065-4fc4-ab25-777adadafb81@glegroupsg2000goo.googlegroups.com>
Date Sat, 16 Jul 2011 09:27:52 +1000
Subject Re: list(), tuple() should not place at "Built-in functions" in documentation
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1088.1310772475.1164.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1310772475 news.xs4all.nl 23977 [2001:888:2000:d::a6]:54057
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9581

Show key headers only | View raw


On Sat, Jul 16, 2011 at 5:53 AM, Inside <fancheyujian@gmail.com> wrote:
> def add(users):
>    assert(users, (tuple, list))
>    #If necessary I'll also check every obj in the sequence to see whether it's a User.
>
> I just follow some coding rules of me:
> 1. Controlling "input" strictly.
> 2. In a function keep doubting on its parameters until they're checked.
> 3. Let potential errors raise as early as possible.

What you're doing there is writing code in Python, not writing Python code.

To be more Pythonic, your code should actually stop caring about
whether something is-a User, and instead simply care about whether or
not it can be treated as a User. (And for Travaglia fans, yes, a User
object WILL have an abuse() method.)

Instead of asserting that the parameter is a User, just add it
cheerfully to your list, and then when you iterate over the list and
call some method on each one, you'll get an exception if one of them
doesn't have that method.

This allows a huge enhancement to polymorphism, in that you no longer
need to worry about what your pointers are; in C++, you can run over a
list of users and ask if they're all Students, but then you need to
cast all those pointers if you're going to then ask them all what
subjects they're studying. In Python, all you do is ask your list of
objects what subjects they're studying - all the students will
respond, and anything that doesn't know what "studying" is will throw
an exception.

The analogy with reality breaks down a bit here. I've seen plenty of
students with no idea of what it means to study. But Python can handle
that too - just 'del' the method in the subclass.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: list(),tuple() should not place at "Built-in functions" in documentation Inside <fancheyujian@gmail.com> - 2011-07-15 12:53 -0700
  Type checking versus polymorphism (was: list(),tuple() should not place at "Built-in functions" in documentation) Ben Finney <ben+python@benfinney.id.au> - 2011-07-16 09:24 +1000
    Re: Type checking versus polymorphism (was: list(),tuple() should not place at "Built-in functions" in documentation) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-16 12:47 +1000
      Re: Type checking versus polymorphism (was: list(), tuple() should not place at "Built-in functions" in documentation) Chris Rebert <clp2@rebertia.com> - 2011-07-15 22:02 -0700
        Re: Type checking versus polymorphism (was: list(), tuple() should not place at "Built-in functions" in documentation) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-16 16:33 +1000
  Re: list(), tuple() should not place at "Built-in functions" in documentation Chris Angelico <rosuav@gmail.com> - 2011-07-16 09:27 +1000
    Liskov substitution principle (was: list(), tuple() should not place at "Built-in functions" in documentation) Ben Finney <ben+python@benfinney.id.au> - 2011-07-16 10:04 +1000
      Re: Liskov substitution principle (was: list(), tuple() should not place at "Built-in functions" in documentation) Chris Angelico <rosuav@gmail.com> - 2011-07-16 10:17 +1000
        Re: Liskov substitution principle Ben Finney <ben+python@benfinney.id.au> - 2011-07-16 11:47 +1000
  Re: list(),tuple() should not place at "Built-in functions" in documentation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-16 14:06 +1000

csiph-web