[Tutor] How to get all methods of "object"?

boB Stepp robertvstepp at gmail.com
Sun Sep 22 01:20:14 EDT 2019


On Sat, Sep 21, 2019 at 9:35 PM Cameron Simpson <cs at cskk.id.au> wrote:
>
> On 22Sep2019 12:12, DL Neil <PyTutor at danceswithmice.info> wrote:
> >On 22/09/19 11:29 AM, boB Stepp wrote:
> >>Python 3.7.4
> >>I tried to do this with:
> >>>>>for attribute in dir(object):
> >>     print(attribute)
> >...
> >
> >>But I know at least one that is not listed:  __dict__
>
> "object" doesn't have a __dict__. Saves space!

Huh?  If I do the following, there clearly seems to be an object.__dict__ :

>>> for key in object.__dict__:
    print('{0:20} {1!r:20}'.format(key, object.__dict__[key]))


__repr__             <slot wrapper '__repr__' of 'object' objects>
__hash__             <slot wrapper '__hash__' of 'object' objects>
__str__              <slot wrapper '__str__' of 'object' objects>
__getattribute__     <slot wrapper '__getattribute__' of 'object' objects>
__setattr__          <slot wrapper '__setattr__' of 'object' objects>
__delattr__          <slot wrapper '__delattr__' of 'object' objects>
__lt__               <slot wrapper '__lt__' of 'object' objects>
__le__               <slot wrapper '__le__' of 'object' objects>
__eq__               <slot wrapper '__eq__' of 'object' objects>
__ne__               <slot wrapper '__ne__' of 'object' objects>
__gt__               <slot wrapper '__gt__' of 'object' objects>
__ge__               <slot wrapper '__ge__' of 'object' objects>
__init__             <slot wrapper '__init__' of 'object' objects>
__new__              <built-in method __new__ of type object at 0x55f6cbb7cba0>
__reduce_ex__        <method '__reduce_ex__' of 'object' objects>
__reduce__           <method '__reduce__' of 'object' objects>
__subclasshook__     <method '__subclasshook__' of 'object' objects>
__init_subclass__    <method '__init_subclass__' of 'object' objects>
__format__           <method '__format__' of 'object' objects>
__sizeof__           <method '__sizeof__' of 'object' objects>
__dir__              <method '__dir__' of 'object' objects>
__class__            <attribute '__class__' of 'object' objects>
__doc__              'The most base type'

(Argh!  Gmail messed up my nicely lined up display.)

> >>And I would not
> >>be surprised if there are others.
>
> Really, Python tries to be very transparent here. If it isn't listed, it
> probably isn't a direct attribute of the class.

I find it interesting that you used the phrase "direct attribute of
the class" instead of just attribute of the class.  While searching
through the Python docs on the side trail __slots__ (Should I now be
babbling insanely? :) ) I stumbled onto
https://docs.python.org/3/reference/datamodel.html?highlight=slots#special-method-names
 After scanning through all of this and trying experiments like:

>>> object.__add__ is not None
Traceback (most recent call last):
  File "<pyshell#125>", line 1, in <module>
    object.__add__ is not None
AttributeError: type object 'object' has no attribute '__add__'
>>> object.__init__ is not None
True
>>> object.__slots__ is not None
Traceback (most recent call last):
  File "<pyshell#127>", line 1, in <module>
    object.__slots__ is not None
AttributeError: type object 'object' has no attribute '__slots__'

I realize that there are other special methods that apparently are not
defined for "object".  I am still pondering this, but am currently
concluding that those methods/attributes that dir() does show must
generally be part of every new object creation unless the coder
specifies those methods to be "None".  However, dir() does not show
the existence of "object.__dict__", so I am probably not fully
understanding everything yet.

> OTOH, the documentation for dir() includes this text:
>
>   Because dir() is supplied primarily as a convenience for use at an
>   interactive prompt, it tries to supply an interesting set of names
>   more than it tries to supply a rigorously or consistently defined set
>   of names

This may explain away some of my concerns.

> >>Basically, I am wondering what are *all* the things inherited from
> >>"object" when one creates one's own class?
>
> Yopu're probably doing the right thing. In the general case you probably
> need to consult every class in the subclass' __mro__ attribute. And
> instead of dir() you might consult the __dict__ or __slots__ attributes;
> note that __getattr__ lets a class offer attributes-on-demand which
> won't show in __dict__ or __slots__.

Ugh.  MRO.  Another headache I've been revisiting while looking at the
articles Mats mentioned in the other thread.  At least MRO and super()
are making more sense today than past efforts at understanding these
concepts.

> >>I am currently trying to find such a list in the Python 3 docs, but so
> >>far I have been unsuccessful.
>
> Also see the "inspect" module.

Haven't made it there yet.

> >An interesting idea...
> >Try creating a class (which inherits the basic "object"), then __dict__
> >(and two other 'extra' attributes) will become visible/pertinent.

Tried this and __dict__, __module__ and __weakref__ show up.  The
first two I have passing familiarity with.  __weakref__ I have no
familiarity with.

> >(I'm not mentioning "slots" in a bid to preserve @boB's sanity)
>
> Too late! In both senses :-)

You guys are so bad!  You both KNOW that I will at least give "slots"
a passing glance.  So far it seems like a nice way to set a fixed
group of allowed attributes without allowing any dynamic adding or
subtracting of attributes.  Which suppresses __dict__ creation and
apparently saving on memory where apparently those memory savings only
seriously add up when lots of class instances are created.  But
apparently more recent 3.x versions have reduced how much memory gets
saved.  But now I'm wondering what all of those "slot wrapper"
prefixes mean when I printed out object.__dict__.  Sigh.


-- 
boB


More information about the Tutor mailing list