Re: interactive help on the base object

Discussion in 'Python' started by Mark Lawrence, Dec 8, 2013.

  1. On 07/12/2013 01:35, Terry Reedy wrote:
    > On 12/6/2013 12:03 PM, Mark Lawrence wrote:
    >> Is it just me, or is this basically useless?
    >>
    >> >>> help(object)

    >> Help on class object in module builtins:
    >>
    >> class object
    >> | The most base type

    >
    > Given that this can be interpreted as 'least desirable', it could
    > definitely be improved.
    >
    >> Surely a few more words,

    >
    > How about something like.
    >
    > '''The default top superclass for all Python classes.
    >
    > Its methods are inherited by all classes unless overriden.
    > '''
    >
    > When you have 1 or more concrete suggestions for the docstring, open a
    > tracker issue.
    >


    Terry's suggestion above remains odds on favourite on the grounds that
    there have been no other suggestions. I'll give it another day, then
    raise a tracker issue, unless the overwhelming smell of pot that has
    been drifting around this thread knocks me unconscious.

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Dec 8, 2013
    #1
    1. Advertising

  2. On Sun, 08 Dec 2013 23:48:57 +0000, Mark Lawrence wrote:

    >>> >>> help(object)
    >>> Help on class object in module builtins:
    >>>
    >>> class object
    >>> | The most base type



    >> '''The default top superclass for all Python classes.
    >> Its methods are inherited by all classes unless overriden.
    >> '''


    > Terry's suggestion above remains odds on favourite on the grounds that
    > there have been no other suggestions. I'll give it another day, then
    > raise a tracker issue, unless the overwhelming smell of pot that has
    > been drifting around this thread knocks me unconscious.


    """ The root class for all Python classes. Its methods are inherited by
    all classes unless overriden. """

    --
    Denis McMahon,
    Denis McMahon, Dec 9, 2013
    #2
    1. Advertising

  3. Mark Lawrence

    Mark Janssen Guest

    >>>> >>> help(object)
    >>>> Help on class object in module builtins:
    >>>>
    >>>> class object
    >>>> | The most base type

    >
    >>> '''The default top superclass for all Python classes.
    >>> Its methods are inherited by all classes unless overriden.
    >>> '''

    >
    > """ The root class for all Python classes. Its methods are inherited by
    > all classes unless overriden. """


    *sits back*.
    --
    MarkJ
    Tacoma, Washington
    Mark Janssen, Dec 9, 2013
    #3
  4. On 09/12/2013 01:09, Mark Janssen wrote:
    >>>>> >>> help(object)
    >>>>> Help on class object in module builtins:
    >>>>>
    >>>>> class object
    >>>>> | The most base type

    >>
    >>>> '''The default top superclass for all Python classes.
    >>>> Its methods are inherited by all classes unless overriden.
    >>>> '''

    >>
    >> """ The root class for all Python classes. Its methods are inherited by
    >> all classes unless overriden. """

    >
    > *sits back*.
    >


    Why? If a newbie is encouraged, as everybody is, to use help at the
    interactive prompt, then surely them seeing "The most base type" when
    typing "help(object)" is of no use to them at all.

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Dec 9, 2013
    #4
  5. On 09/12/2013 00:45, Denis McMahon wrote:
    > On Sun, 08 Dec 2013 23:48:57 +0000, Mark Lawrence wrote:
    >
    >>>> >>> help(object)
    >>>> Help on class object in module builtins:
    >>>>
    >>>> class object
    >>>> | The most base type

    >
    >
    >>> '''The default top superclass for all Python classes.
    >>> Its methods are inherited by all classes unless overriden.
    >>> '''

    >
    >> Terry's suggestion above remains odds on favourite on the grounds that
    >> there have been no other suggestions. I'll give it another day, then
    >> raise a tracker issue, unless the overwhelming smell of pot that has
    >> been drifting around this thread knocks me unconscious.

    >
    > """ The root class for all Python classes. Its methods are inherited by
    > all classes unless overriden. """
    >


    Thanks Denis, you've reminded me why I asked in the first place. What
    methods, if any does it provide? Are they all abstract? etc???
    Personally I'm not really interested, but a newbie might well be and
    hence might wonder what the hell is going on. If and only if the
    situation can be improved I'll raise an issue, if not I'll quietly let
    it drop, and consider more important things, like why are Australia
    currently thrashing England at cricket? :)

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Dec 9, 2013
    #5
  6. On Sun, 08 Dec 2013 23:48:57 +0000, Mark Lawrence wrote:

    > Terry's suggestion above remains odds on favourite on the grounds that
    > there have been no other suggestions. I'll give it another day, then
    > raise a tracker issue,



    It's not merely the default superclass, it *is* the superclass to
    everything. In Python 3, you cannot create an object that doesn't derive
    from object. (At least not in pure Python -- perhaps you could do so in a
    C extension class?)

    "Top" is misleading, because it assumes that class diagrams are always
    drawn with ancestors at the top and descendants at the bottom.

    No need to say that methods are inherited unless overridden, it goes
    without saying that you can override methods.


    object:

    The most fundamental base class for all Python classes and the root of
    the class inheritance hierarchy. All classes inherit from object.



    --
    Steven
    Steven D'Aprano, Dec 9, 2013
    #6
  7. On Mon, 09 Dec 2013 01:43:43 +0000, Mark Lawrence wrote about object:

    > What methods, if any does it provide? Are they all abstract? etc???



    Pretty much nothing useful :)

    py> dir(object)
    ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
    '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
    '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
    '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
    '__subclasshook__']


    What few methods there are typically do nothing, or nothing interesting.
    A few implement basic functionality, e.g. __eq__ performs equality based
    on identity (an object is equal to itself and nothing else).



    --
    Steven
    Steven D'Aprano, Dec 9, 2013
    #7
  8. Mark Lawrence

    Mark Janssen Guest

    >> 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?

    --
    MarkJ
    Tacoma, Washington
    Mark Janssen, Dec 9, 2013
    #8
  9. 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
    Chris Angelico, Dec 9, 2013
    #9
  10. Mark Lawrence

    rusi Guest

    On Monday, December 9, 2013 8:11:47 AM UTC+5:30, zipher 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?


    Thats right. What does a collection object like [] or the empty set ∅
    (assuming the unicode-gods will allow and bless that) do when it
    collects nothing? Lets make sure all lists and sets start non-empty.

    And why have a stupid number like 0 when it counts nothing?
    Lets go back to roman numerals which is so much more…


    > Practicality-beats-purity-yours?


    Practical!!⸮¿¡

    PS Can some kind soul inform me whether I could convince GG to unicode my post?
    rusi, Dec 9, 2013
    #10
  11. On Sun, 08 Dec 2013 18:58:09 -0800, rusi wrote:


    > PS Can some kind soul inform me whether I could convince GG to unicode
    > my post?


    Does GG not give you some way of inspecting the post's full headers?

    Anyway, here you go:

    Content-Type: text/plain; charset=UTF-8


    Your plan succeeded.

    Personally, I wouldn't stress too much about this. While it would be
    nice, and desirable, for GG to always use UTF-8 instead of picking a
    different encoding based on the phase of the moon, the main thing is that
    it picks a valid encoding. So far I see no reason to accuse GG of using
    an invalid encoding. Valid but legacy encodings are better than
    mojibake :)


    --
    Steven
    Steven D'Aprano, Dec 9, 2013
    #11
  12. Mark Lawrence

    rusi Guest

    Thanks for the info.

    On Monday, December 9, 2013 9:46:30 AM UTC+5:30, Steven D'Aprano wrote:
    > On Sun, 08 Dec 2013 18:58:09 -0800, rusi wrote:


    > > PS Can some kind soul inform me whether I could convince GG to unicode
    > > my post?


    > Does GG not give you some way of inspecting the post's full headers?


    Well I spent half hour looking around -- both inside GG and of course
    searching before asking.

    > Anyway, here you go:


    > Content-Type: text/plain; charset=UTF-8


    > Your plan succeeded.


    > Personally, I wouldn't stress too much about this. While it would be
    > nice, and desirable, for GG to always use UTF-8 instead of picking a
    > different encoding based on the phase of the moon, the main thing is that
    > it picks a valid encoding. So far I see no reason to accuse GG of using
    > an invalid encoding. Valid but legacy encodings are better than
    > mojibake :)


    Heh! I'm hardly a heavy-duty user of unicode -- just keeping track of bugs
    and workarounds. I am of course using 'bug' in a wide pragmatic sense of
    preventing communication.

    Mojibake is a technical problem.
    Non (human) communication is a more fundamental problem.
    Keeping an eye on the latter is (for me) a bigger issue than the former.
    rusi, Dec 9, 2013
    #12
  13. Mark Lawrence

    Terry Reedy Guest

    On 12/8/2013 8:43 PM, Mark Lawrence wrote:
    > On 09/12/2013 00:45, Denis McMahon wrote:
    >> On Sun, 08 Dec 2013 23:48:57 +0000, Mark Lawrence wrote:
    >>
    >>>>> >>> help(object)
    >>>>> Help on class object in module builtins:
    >>>>>
    >>>>> class object
    >>>>> | The most base type

    >>
    >>
    >>>> '''The default top superclass for all Python classes.
    >>>> Its methods are inherited by all classes unless overriden.
    >>>> '''


    I said 'top' instead of 'bottom' or 'base' to loosen up thinking a bit.
    I did not expect Mark to make a mound out of that flip.

    >>> Terry's suggestion above remains odds on favourite on the grounds that
    >>> there have been no other suggestions. I'll give it another day, then
    >>> raise a tracker issue, unless the overwhelming smell of pot that has
    >>> been drifting around this thread knocks me unconscious.

    >>
    >> """ The root class for all Python classes. Its methods are inherited by
    >> all classes unless overriden. """


    'Root' is even better, since it does not depend on whether a tree is
    drawn up or down. Thanks.

    > Thanks Denis, you've reminded me why I asked in the first place. What
    > methods, if any does it provide?


    Good question.
    >>> dir(object)

    ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
    '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
    '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
    '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
    '__subclasshook__']

    > Are they all abstract? etc???


    No.

    > Personally I'm not really interested, but a newbie might well be and
    > hence might wonder what the hell is going on.


    For everything else, help lists the special name methods directly
    associated with the object, along with docstrings.

    >>> help(C)

    Help on class C in module __main__:

    class C(builtins.object)
    | Data descriptors defined here:
    |
    | __dict__
    | dictionary for instance variables (if defined)
    |
    | __weakref__
    | list of weak references to the object (if defined)

    I think help should do the same for object

    >>> object.__hash__.__doc__

    'x.__hash__() <==> hash(x)'

    is equivalent to

    | __abs__(...)
    | x.__abs__() <==> abs(x)

    etc printed for help(int)

    The fact that __dict__ does not exist for object but is only added for
    subclasses explains why one must subclass object to get instances that
    allow attributes.

    >>> o = object()
    >>> o.a = 1

    Traceback (most recent call last):
    File "<pyshell#8>", line 1, in <module>
    o.a = 1
    AttributeError: 'object' object has no attribute 'a'
    >>> c = C()
    >>> c.a=1


    > If and only if the
    > situation can be improved I'll raise an issue


    I think it can be. If you prefer me to open the issue, say so.
    We should look for existing issues, and closed issues that rejected change.

    --
    Terry Jan Reedy
    Terry Reedy, Dec 9, 2013
    #13
  14. Mark Lawrence

    Guest

    On 12/08/2013 09:46 PM, rusi wrote:
    > On Monday, December 9, 2013 9:46:30 AM UTC+5:30, Steven D'Aprano wrote:
    >> On Sun, 08 Dec 2013 18:58:09 -0800, rusi wrote:

    >[...]
    >> Does GG not give you some way of inspecting the post's full headers?

    >
    > Well I spent half hour looking around -- both inside GG and of course
    > searching before asking.


    If you click on the little "down" triangle to the right
    of the "reply" button for a message, you'll get a menu
    that includes "Show Original". You can see the headers
    including the "Content-Type:" in that original.

    Of course this means you have to post first and find out
    after...
    , Dec 9, 2013
    #14
  15. On Sun, 08 Dec 2013 18:41:47 -0800, 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?


    You cut out the part of my post where I explained that object does in
    fact provide a minimal set of useful functionality. The example I gave
    was __eq__ (equal), but there is also __ne__ (not equal), __hash__
    (hashing), __str__ and __repr__ (although many classes will wish to
    override them) and a few more. They're useful, even necessary, but hardly
    exciting.

    The other reasons for inheriting from object include:

    - If all classes are part of a single hierarchy, it must logically end at
    one (or more, if you support multiple inheritance, which Python does)
    bases classes. (Unless there are loops, which are generally prohibited in
    all OOP systems I know of). The simplest way to do this is with a single
    base class.

    - Pragmatism: it was the easiest way to unify types and classes way back
    in Python 2.2, prior to which there was no single hierarchy and all
    classes were (in a sense) distinct.

    Pedants will of course realise that Python 2 has *two* object
    hierarchies, classic classes which don't share a base class, and types
    (a.k.a. "new style classes") which do. In Python 3, classic classes are
    gone, and there is a single class hierarchy with object the base.


    There may be other reasons.



    --
    Steven
    Steven D'Aprano, Dec 9, 2013
    #15
  16. Mark Lawrence

    rusi Guest

    On Monday, December 9, 2013 10:56:28 AM UTC+5:30, wrote:
    > On 12/08/2013 09:46 PM, rusi wrote:
    > > On Monday, December 9, 2013 9:46:30 AM UTC+5:30, Steven D'Aprano wrote:
    > >> On Sun, 08 Dec 2013 18:58:09 -0800, rusi wrote:

    > >[...]
    > >> Does GG not give you some way of inspecting the post's full headers?

    > > Well I spent half hour looking around -- both inside GG and of course
    > > searching before asking.


    > If you click on the little "down" triangle to the right
    > of the "reply" button for a message, you'll get a menu
    > that includes "Show Original". You can see the headers
    > including the "Content-Type:" in that original.


    Thanks rurpy -- I only looked for how one may set and not just what is.

    So now I look at my own post in GG and see

    Content-Type: text/plain; charset=UTF-8

    So far so good.

    However when I point firefox at my own post in the archive
    https://mail.python.org/pipermail/python-list/2013-December/662015.html

    firefox shows encoding as Windows-1252.

    Note Ive looked at a dozen random pages and for all FF shows encoding as
    utf-8 except the python list archive ones which show as Win 1252

    Note looking into the html I see
    <META http-equiv="Content-Type" content="text/html; charset=us-ascii">

    How us-ascii becomes Win-1252 is outside the reach of my meagre intelligence!

    Though I still suspect something is not quite right with the python
    mailing-list and/or archive in respect of char encodings.

    [Am I beginning to sound like jmf is my guru :) ]
    rusi, Dec 9, 2013
    #16
  17. Mark Lawrence

    Alan Bawden Guest

    Steven D'Aprano <> writes:

    > - If all classes are part of a single hierarchy, it must logically end at
    > one (or more, if you support multiple inheritance, which Python does)
    > bases classes. (Unless there are loops, which are generally prohibited in
    > all OOP systems I know of). The simplest way to do this is with a single
    > base class.


    The original Lisp Machine "Flavors" system (one of the ancestors of the
    Common Lisp Object System) allowed inheritance to be an arbitrary
    directed graph -- possibly with cycles.

    I don't believe that this was done for any deep principled reason, but
    rather it was just permitted because the algorithm for computing method
    resolution order didn't actually care whether there were inheritance
    cycles -- it still terminated and returned an ordered list of component
    classes.

    I do not remember seeing any code that made use of this ability, so
    don't ask me if it was good for anything, but it was definitely there...

    --
    Alan Bawden
    Alan Bawden, Dec 9, 2013
    #17
  18. On Mon, Dec 9, 2013 at 6:31 PM, Alan Bawden
    <> wrote:
    > I don't believe that this was done for any deep principled reason, but
    > rather it was just permitted because the algorithm for computing method
    > resolution order didn't actually care whether there were inheritance
    > cycles -- it still terminated and returned an ordered list of component
    > classes.


    How does that work, exactly? How do you have a class inherit
    (ultimately) from itself, and how does that impact the component class
    list?

    ChrisA
    Chris Angelico, Dec 9, 2013
    #18
  19. On 09/12/2013 06:44, rusi wrote:
    > On Monday, December 9, 2013 10:56:28 AM UTC+5:30, wrote:
    >> On 12/08/2013 09:46 PM, rusi wrote:
    >>> On Monday, December 9, 2013 9:46:30 AM UTC+5:30, Steven D'Aprano wrote:
    >>>> On Sun, 08 Dec 2013 18:58:09 -0800, rusi wrote:
    >>> [...]
    >>>> Does GG not give you some way of inspecting the post's full headers?
    >>> Well I spent half hour looking around -- both inside GG and of course
    >>> searching before asking.

    >
    >> If you click on the little "down" triangle to the right
    >> of the "reply" button for a message, you'll get a menu
    >> that includes "Show Original". You can see the headers
    >> including the "Content-Type:" in that original.

    >
    > Thanks rurpy -- I only looked for how one may set and not just what is.
    >
    > So now I look at my own post in GG and see
    >
    > Content-Type: text/plain; charset=UTF-8
    >
    > So far so good.
    >
    > However when I point firefox at my own post in the archive
    > https://mail.python.org/pipermail/python-list/2013-December/662015.html
    >
    > firefox shows encoding as Windows-1252.
    >
    > Note Ive looked at a dozen random pages and for all FF shows encoding as
    > utf-8 except the python list archive ones which show as Win 1252
    >
    > Note looking into the html I see
    > <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
    >
    > How us-ascii becomes Win-1252 is outside the reach of my meagre intelligence!
    >
    > Though I still suspect something is not quite right with the python
    > mailing-list and/or archive in respect of char encodings.
    >
    > [Am I beginning to sound like jmf is my guru :) ]
    >


    I'd mention that I never seem to have a problem using Thunderbird on
    Windows 7, but I won't as I don't want to be accused of bullying, hating
    GG, or whatever. Doh!!! :)

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Dec 9, 2013
    #19
  20. On 09/12/2013 05:00, Terry Reedy wrote:
    >
    > I think it can be. If you prefer me to open the issue, say so.
    > We should look for existing issues, and closed issues that rejected change.
    >


    Thanks for the offer Terry and yes, please open an issue.

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, Dec 9, 2013
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Lawrence

    interactive help on the base object

    Mark Lawrence, Dec 6, 2013, in forum: Python
    Replies:
    25
    Views:
    179
    alex23
    Dec 11, 2013
  2. Devin Jeanpierre

    Re: interactive help on the base object

    Devin Jeanpierre, Dec 8, 2013, in forum: Python
    Replies:
    0
    Views:
    59
    Devin Jeanpierre
    Dec 8, 2013
  3. Terry Reedy

    Re: interactive help on the base object

    Terry Reedy, Jan 17, 2014, in forum: Python
    Replies:
    0
    Views:
    62
    Terry Reedy
    Jan 17, 2014
  4. Mark Lawrence

    Re: interactive help on the base object

    Mark Lawrence, Jan 17, 2014, in forum: Python
    Replies:
    0
    Views:
    47
    Mark Lawrence
    Jan 17, 2014
  5. Terry Reedy

    Re: interactive help on the base object

    Terry Reedy, Jan 17, 2014, in forum: Python
    Replies:
    0
    Views:
    65
    Terry Reedy
    Jan 17, 2014
Loading...

Share This Page