[Baypiggies] Descriptors with metaclasses don't quite seem to work
Aseem Mohanty
ams.fwd at gmail.com
Thu Jan 3 02:13:12 EST 2019
Ah NM I is stupid late in the evening. __call__ needs to return the
instance.
On Wed, Jan 2, 2019 at 10:38 PM Aseem Mohanty <ams.fwd at gmail.com> wrote:
> Hi All.
>
> I have the following code where I am adding a metaclass to a descriptor.
> This is a reduction case so the metaclass really does nothing.
>
> Running this with python3 does not appear to trigger the descriptors
> __get__ or __set__. Without the metaclass things work as expected. I am not
> exactly sure why having the metaclass would not work as all the attributes
> for the descriptor appear to be preserved.
>
> Any help would be greatly appreciated.
>
> TIA
> AM
>
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> class Meta(type):
>
> def __init__(cls, name, bases, attrs):
> print('meta init')
> super().__init__(name, bases, attrs)
>
> def __call__(cls, *args, **kwargs):
> print('meta call')
> inst = super().__call__()
>
> class X(metaclass=Meta):
>
> def __init__(self):
> print('init X')
> self._val = None
>
> def __get__(self, obj, obj_type):
> print('getting value')
> return self._val
>
> def __set__(self, obj, value):
> print('-- setting value')
> self._val = value
>
> class M:
> x = X()
>
> m = M()
> m.x = 1
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20190102/499f9288/attachment.html>
More information about the Baypiggies
mailing list