Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'root': 0.05; '(except': 0.07; 'subject:help': 0.08; "'a'": 0.09; 'methods,': 0.09; 'python:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'cc:name:python list': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inheritance': 0.16; 'janssen': 0.16; 'object()': 0.16; 'repetition': 0.16; 'sorts': 0.16; 'subject:object': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'pieces': 0.19; 'work,': 0.20; '>>>': 0.22; 'putting': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'dec': 0.30; 'list:': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'steven': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'maybe': 0.34; 'subject:the': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'useful': 0.36; 'two': 0.37; 'pm,': 0.38; 'does': 0.39; 'itself': 0.39; 'most': 0.60; 'mentioned': 0.61; 'simple': 0.61; 'default': 0.69; 'therefore': 0.72; 'lowest': 0.74; 'functions:': 0.84; 'subject:base': 0.84; 'to:none': 0.92; 'directly.': 0.95; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=1pwwpbstzkj1nz+9DXK2qYyOUvrnm9KksclwXF0vSIY=; b=CxQcVtNqv1KlYbCskpbScE4PcYb5VxD2FgloZ//GYGZOoGsxevEr1CCOIxkqIFIHXr dVvuQgyHpF3bAH/Z/AKBWPq2uZOWI4XJTE/vJyT3O6Ax2t7e9Ki9PCmNaPlajKH9jxkJ K9g4w3FLSrZB6r0Hk6ZUbVN3zh2x7zp8Fq4wG/E08t+eCyPG3k1MaPF8DdH+I53E+g/y XZARKYw/mLAFZ3G0c6iZV9LuGmevTn9E8hBJfe5UOf3Ua+WQpE98qGzPxbt/EuXNijju Zu1uUxWenwDwjBLhZ18G13sTfEYSVufg5LREbesVrzAInJbzHwFrFwWDhZM9BWfoWSwS xWKA== MIME-Version: 1.0 X-Received: by 10.68.111.33 with SMTP id if1mr18266437pbb.31.1386557848820; Sun, 08 Dec 2013 18:57:28 -0800 (PST) In-Reply-To: References: <52a52b98$0$2762$c3e8da3$76491128@news.astraweb.com> Date: Mon, 9 Dec 2013 13:57:28 +1100 Subject: Re: interactive help on the base object From: Chris Angelico Cc: Python List Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386557858 news.xs4all.nl 2858 [2001:888:2000:d::a6]:58046 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61349 On Mon, Dec 9, 2013 at 1:41 PM, Mark Janssen wrote: >>> What methods, if any does it provide? Are they all abstract? etc??? >> >> Pretty much nothing useful :-) >> >> py> dir(object) >> [...] >> > > So (prodding the student), Why does everything inherit from Object if > it provides no functionality? > > Practicality-beats-purity-yours? Nothing useful to call directly. An int has some useful methods in Python: >>> (258).to_bytes(2,"little") b'\x02\x01' So does a list: >>> [1,4,1,5,9].count(1) 2 But there's nothing you'd normally want to call from object itself (except maybe __repr__). There *are*, however, important pieces of default functionality. Steven mentioned __eq__, and there's also its pair __hash__. The default system works because the root type provides implementations of those two functions: >>> a = object() >>> b = object() >>> a == b False >>> d = {a:"A", b:"B"} >>> d[a] 'A' And it's important that these sorts of things work, because otherwise a simple Python class would look like this: class Foo: def __new__(self): pass def __init__(self): pass def __hash__(self): return id(self) def __eq__(self, other): return self is other # ... This repetition is exactly what inheritance is good at solving. Therefore putting that functionality into a base class makes sense; and since everything MUST have these functions to be able to be used plausibly, putting them in the lowest base class of all makes the most sense. ChrisA