[Tutor] How to get all methods of "object"?
Cameron Simpson
cs at cskk.id.au
Sun Sep 22 04:34:50 EDT 2019
On 22Sep2019 00:20, boB Stepp <robertvstepp at gmail.com> wrote:
>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]))
Hmm. Interesting. Sorry: the "object" class has a __dict__. But
instances of that class do not:
>>> o=object()
>>> dir(o)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__le__', '__lt__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__']
>>> o.__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__dict__'
>>> o.__slots__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__slots__'
>__repr__ <slot wrapper '__repr__' of 'object' objects>
>__hash__ <slot wrapper '__hash__' of 'object' objects>
[...]
>(Argh! Gmail messed up my nicely lined up display.)
Bah! My mutt renders it nicely here! Terminal based mail readers FTW!
>> >>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.
I'm just trying to be clear, avoiding confusion with attribute of an
instance 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".
"object" and the other builtin types often have C implementations (in
CPython) and so their methods are a bit more magic that pure python
classes/types.
> 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.
Object and friends are more basic and magic (or, if you like, less fully
cononically implemented).
>> 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.
It is handy for inspecting objects (eg method call signatures, the
module where something was defined, etc). It may not provide more
insight, just ways to look at things.
>> >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.
__weakref__ is used for "weak references", which don't contribute to an
object's reference count (and thus don't prevent an object being garbage
collected). Handy for keeping registries of things without forcing
objects to stay allocated as a side effect (eg when the object becomes
unused _except_ in that registry).
Not heavily used.
>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.
It is also faster, since the implementation can translate attribute
names to a flat offset instead ofdereferencing through a dictionary.
> 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.
Just think of them as space efficient inflexible objects.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list