Making Classes Subclassable
Steven D'Aprano
steve at pearwood.info
Tue Jul 5 11:33:13 EDT 2016
On Wed, 6 Jul 2016 12:41 am, Ian Kelly wrote:
> On Tue, Jul 5, 2016 at 2:31 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Monday 04 July 2016 18:34, Lawrence D’Oliveiro wrote:
>>
>>> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>>>> --> "type(obj)" or "obj.__class__" (there are small differences)
>>>> give you the type/class of "obj".
>>>
>>> When would it not be the same?
>>
>>
>> class X(object):
>> def __getattribute__(self, name):
>> if __name__ == '__class__':
>> return int
>> return super().__getattribute__(name)
>
> Did you actually test that?
Er, no, because if I had, I would have discovered the silly typo: __name__
instead of name, which screws up the whole thing...
Trying again:
py> class X(object):
... def __getattribute__(self, name):
... if name == '__class__': # NOTE SPELLING
... return int
... return super().__getattribute__(name)
...
py> x = X()
py> x.__class__
<class 'int'>
[...]
> Certain attributes like __class__ and __dict__ are special.
Indeed they are, but __class__ is not *that* special :-)
--
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list