python - Where do classes get their default '__dict__' attributes from? -
if compare lists generated applying dir() built-in object superclass , 'dummy', bodyless class, such as
class a(): pass we find class has 3 attributes ('__dict__', '__module__' , '__weakref__') not present in object class.
where class inherit these additional attributes from?
the
__dict__attribute created internal code intype.__new__. class's metaclass may influence eventual content of__dict__. if using__slots__, have no__dict__attribute.__module__set when class compiled, instance inherits attribute class. can verify performing testinst.__module__ cls.__module__(giveninstinstance ofcls), , modifyingcls.__module__, observinginst.__module__reflects change.(you can stupid things setting
inst.__module__differing valuecls.__module__. not sure why isn't readonly attribute.)__weakref__created when instance is; afaik it's controlled internal cpython object-creation system. attribute present on instances ofobjectsubclasses -- example instances ofint,list,set,tuplehave no__weakref__attribute.in interpreting explanation, keep in mind
class c:equivalentclass c(object):python3. python2, plainclass c:generate instances without__weakref__(or number of other attributes acquiredobjectimplementation).
Comments
Post a Comment