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__
(giveninst
instance 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 ofobject
subclasses -- example instances ofint
,list
,set
,tuple
have 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 acquiredobject
implementation).
Comments
Post a Comment