Richard Oudkerk added the comment:
> Well, what I am doing is more or less the equivalent of
>
> return object.__slots__ if hasattr(object, '__slots') else object.__dict__
>
> and this is coherent with the updated documentation. The one you
> proposed is an alternative behavior; am I supposed to follow that one?
Ignoring some slots but not others would be confusing. I would be inclined to
just leave vars() alone. Maybe a Python implementation could be put in
inspect.py instead.
A possible implementation (which can't be used to modify the object) might be:
import copyreg
def fullvars(obj):
cls = type(obj)
try:
slotnames = cls.__dict__['__slotnames__']
except (KeyError, AttributeError):
slotnames = copyreg._slotnames(cls)
try:
d = vars(obj).copy()
except TypeError:
d = {}
for name in slotnames:
try:
d[name] = getattr(obj, name)
except AttributeError:
pass
return d
class A:
__slots__ = 'x', 'y'
class B(A):
__slots__ = 'u', 'v'
class C(B):
pass
a = A()
a.x = 1
print(fullvars(a)) # {'x': 1}
b = B()
b.x = 2; b.u = 3
print(fullvars(b)) # {'u': 3, 'x': 2}
c = C()
c.y = 4; c.r = 5
print(fullvars(c)) # {'y': 4, 'r': 5}
BTW, I before should have written
try:
slotnames = cls.__dict__['__slotnames__']
except (KeyError, AttributeError):
slotnames = copyreg._slotnames(cls)
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue13290>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com