How does a method of a subclass become a method of the base class?
aapost
aapost at idontexist.club
Sun Mar 26 14:42:14 EDT 2023
On 3/26/23 13:43, Jen Kris wrote:
>
> My question is: what makes "choose_method" a method of the base class, called as self.choose_method instead of UrnaryConstraint.choose_method? Is it super(UrnaryConstraint, self).__init__(strength) or just the fact that Constraint is its base class?
When referring to "self" you are referring to an "instance" of the class
or classes, think in terms of objects (the instance, usually self) vs a
blueprint (the class, usually cls).
saying self.choose_method(mark) checks the "instance" of self to see if
it has something called choose_method at run time (instances being
created at run time). If you have an instance of just Constraint, and
never had a choose_method defined for it, you will get an error because
it can't find it (which in this case is presumed as designed). If the
"instance" is of a subclass of Constraint that does have a
choose_method, that also inherits the stuff from Constraint, it will
successfully call it against the "instance" that has been constructed
with attributes of both classes.
if the instance of a subclass has a definition for something that is in
the base class, for instance IF UrnaryConstraint had it's own def
satisfy() method, the instance would call the subclass version. In the
case given, it does not, so it looks to the parent to see if it has
inherited satisfy() from higher up.
Hopefully that helps a little bit.. just have to get a feel for OO
instantiated object vs class..
More information about the Python-list
mailing list