Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61349
| References | (2 earlier) <mailman.3748.1386546562.18130.python-list@python.org> <l833rs$jik$1@dont-email.me> <mailman.3752.1386553439.18130.python-list@python.org> <52a52b98$0$2762$c3e8da3$76491128@news.astraweb.com> <CAMjeLr_PGfGv_uYbuH6Li77bFG-5FCi9GEiLevqjrfAD5SDbAA@mail.gmail.com> |
|---|---|
| Date | 2013-12-09 13:57 +1100 |
| Subject | Re: interactive help on the base object |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3755.1386557858.18130.python-list@python.org> (permalink) |
On Mon, Dec 9, 2013 at 1:41 PM, Mark Janssen <dreamingforward@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: interactive help on the base object Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-08 23:48 +0000
Re: interactive help on the base object Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-09 00:45 +0000
Re: interactive help on the base object Mark Janssen <dreamingforward@gmail.com> - 2013-12-08 17:09 -0800
Re: interactive help on the base object Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-09 01:38 +0000
Re: interactive help on the base object Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-09 01:43 +0000
Re: interactive help on the base object Steven D'Aprano <steve@pearwood.info> - 2013-12-09 02:31 +0000
Re: interactive help on the base object Mark Janssen <dreamingforward@gmail.com> - 2013-12-08 18:41 -0800
Re: interactive help on the base object rusi <rustompmody@gmail.com> - 2013-12-08 18:58 -0800
Re: interactive help on the base object Steven D'Aprano <steve@pearwood.info> - 2013-12-09 04:16 +0000
Re: interactive help on the base object rusi <rustompmody@gmail.com> - 2013-12-08 20:46 -0800
Re: interactive help on the base object rurpy@yahoo.com - 2013-12-08 21:26 -0800
Re: interactive help on the base object rusi <rustompmody@gmail.com> - 2013-12-08 22:44 -0800
Re: interactive help on the base object Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-09 09:44 +0000
Re: interactive help on the base object Steven D'Aprano <steve@pearwood.info> - 2013-12-09 05:44 +0000
Re: interactive help on the base object Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2013-12-09 02:31 -0500
Re: interactive help on the base object Chris Angelico <rosuav@gmail.com> - 2013-12-09 18:39 +1100
Re: interactive help on the base object Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2013-12-10 00:34 -0500
Re: interactive help on the base object Chris Angelico <rosuav@gmail.com> - 2013-12-10 16:44 +1100
Re: interactive help on the base object Chris Angelico <rosuav@gmail.com> - 2013-12-09 13:57 +1100
Re: interactive help on the base object Terry Reedy <tjreedy@udel.edu> - 2013-12-09 00:00 -0500
Re: interactive help on the base object Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-09 23:48 +0000
Re: interactive help on the base object Steven D'Aprano <steve@pearwood.info> - 2013-12-09 01:58 +0000
csiph-web