class SoftDeleteMixin(object):
deleted_at = Column(DateTime)
deleted = Column(types.SoftDeleteInteger, default=0)
def soft_delete(self, session):
"""Mark this object as deleted."""
self.deleted = self.id
self.deleted_at = timeutils.utcnow()
self.save(session=session)
In class SoftDeleteMixin method soft_delete, it references nonexistent self.id and self.save. Why can it do that in python?
Note: the focus is the class can reference nonexistent variable and method, not that it is a Mixin class.
soft_delete? You'll get anAttributeError.AttributeError. Note, the class seems to be intended to be used as a mixin, so whatever inherits from it likely is supposed to have those attributesselfis just a reference to an instance, the instance that the method is being called on. Does that instance have anid? a.save? If it doesn't, it will throw anAttributeError, if it does, it will not.selfin Python is the same asthisin Java. The issue at hand here is that inheritance in Python is very, very, very, very different than inheritance in Java.