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 in type.__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 test inst.__module__ cls.__module__ (given inst instance of cls), , modifying cls.__module__ , observing inst.__module__ reflects change.

    (you can stupid things setting inst.__module__ differing value cls.__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 of object subclasses -- example instances of int, list, set, tuple have no __weakref__ attribute.

    in interpreting explanation, keep in mind class c: equivalent class c(object): python3. python2, plain class c: generate instances without __weakref__ (or number of other attributes acquired object implementation).


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -