[Tutor] How to get all methods of "object"?
Cameron Simpson
cs at cskk.id.au
Sat Sep 21 23:44:52 EDT 2019
On 22Sep2019 14:49, DL Neil <PyTutor at danceswithmice.info> wrote:
>On 22/09/19 2:35 PM, Cameron Simpson 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)
>>>...
>
>>>By definition, doesn't "inheritance" mean that the sub-class
>>>includes *all* of the attributes (data and code) of its
>>>super-class(es)?
>>
>>Unless overridden, yeah. But they don't all
>
>Incomplete sentence? Do I sense a master-class in the offing?
Incomplete sentence yes. Master class no.
I think I was considering mentioning "attributes presented via
__getattr__/__getattribute__, which aren't visible in
__slots__/__dict__, and aren't "listable" - you can only probe for them
by trying to access the attribute. Which is why classes can define a
__dir__ dunder method which implements dir(), so that dir() can mention
extra things like that.
>Surely though, if an attribute is part of a SuperClass then it will
>'appear' in the SubClass. If SubClass.x overrides SuperClass.x (or x()
>) then attribute-x still appears, even if it contains different data,
>a different type, or different code?
Sure. (Of course one could implement __getattribute__ or a property
which raises AttributeError to conceal an inherited attribute, but that
would be perverse.)
>Granted that SuperClass.x is 'invisible' if SubClass.x has been
>defined, but even so, instead of self.x can't we still access
>SuperClass.x?
Yeah. As: SuperClass.x(self,...) for a method. But remember that these
are _class_ attributes, not instance attributes. Thus:
>>> class A:
... attr = 'foo'
...
>>> class B(A):
... attr = 'bah'
...
>>> x = B()
>>> x.attr
'bah'
>>> x.attr = 'zot'
>>> x.attr
'zot'
>>> B.attr
'bah'
>>> A.attr
'foo'
>(I can't think of a time when I've tried, but...)
>Is there another case when a super class's attribute will not be
>inherited.
>(again: excluding slots or other sleigh-of-hand)
Don't think so.
Cheers,
Cameron Simpson <cs at cskk.id.au>
in rec.moto, jsh wrote:
> Dan Nitschke wrote:
> > Ged Martin wrote:
> > > On Sat, 17 May 1997 16:53:33 +0000, Dan Nitschke scribbled:
> > > >(And you stay *out* of my dreams, you deviant little
> > > >weirdo.)
> > > Yeah, yeah, that's what you're saying in _public_....
> > Feh. You know nothing of my dreams. I dream entirely in text (New Century
> > Schoolbook bold oblique 14 point), and never in color. I once dreamed I
> > was walking down a flowchart of my own code, and a waterfall of semicolons
> > was chasing me. (I hid behind a global variable until they went by.)
> You write code in a proportional serif? No wonder you got extra
> semicolons falling all over the place.
No, I *dream* about writing code in a proportional serif font.
It's much more exciting than my real life.
/* dan: THE Anti-Ged -- Ignorant Yank (tm) #1, none-%er #7 */
Dan Nitschke peDANtic at best.com nitschke at redbrick.com
More information about the Tutor
mailing list