Message171897
> 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) |
|
Date |
User |
Action |
Args |
2012-10-03 17:01:48 | sbt | set | recipients:
+ sbt, rhettinger, terry.reedy, eric.araujo, maker, JBernardo, serhiy.storchaka |
2012-10-03 17:01:47 | sbt | set | messageid: <1349283707.89.0.868851734938.issue13290@psf.upfronthosting.co.za> |
2012-10-03 17:01:47 | sbt | link | issue13290 messages |
2012-10-03 17:01:47 | sbt | create | |
|