My question is: what makes "choose_method" a method of the base class=
, called as self.choose_method instead of UrnaryConstraint.choose_method?
The base class:
class Constraint(object):[...]
def satisfy(self, mark):[...]
       global planner
       self.choose_method(mark)
The subclass:
class UrnaryConstraint(Constraint):
   def choose_method(self, mark):
       if self.my_output.mark != mark and \
          Strength.stronger(self.strength, self.my_output.walk_strength):
self.satisfied = True
       else:
           self.satisfied = False
The base class Constraint doesn’t have a "choose_method" class method,
but it’s called as self.choose_method(mark) on the final line of
Constraint shown above.Â
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?Â
Also, this program also has a class BinaryConstraint that is also a...
subclass of Constraint and it also has a choose_method class method
that is similar but not identical:
When called from Constraint, it uses the one at UrnaryConstraint. How
does it know which one to use?Â
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?
On 3/26/23 1:43 PM, Jen Kris via Python-list wrote:class?
The base class:
class Constraint(object):
def __init__(self, strength):
       super(Constraint, self).__init__()
       self.strength = strength
def satisfy(self, mark):
       global planner
       self.choose_method(mark)
The subclass:
class UrnaryConstraint(Constraint):
def __init__(self, v, strength):
       super(UrnaryConstraint, self).__init__(strength)
       self.my_output = v
       self.satisfied = False
       self.add_constraint()
   def choose_method(self, mark):
       if self.my_output.mark != mark and \
          Strength.stronger(self.strength, self.my_output.walk_strength):
self.satisfied = True
       else:
           self.satisfied = False
The base class Constraint doesn’t have a "choose_method" class method, but it’s called as self.choose_method(mark) on the final line of Constraint shown above.
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
built, and all the methods of that class are put into the object, then the next level, and so on, and if a duplicate method is found, it just overwrites the connection. Then when the object is used, we see if there is a method by that name to use, so
Also, this program also has a class BinaryConstraint that is also a subclass of Constraint and it also has a choose_method class method that is similar but not identical:
def choose_method(self, mark):
   if self.v1.mark == mark:
           if self.v2.mark != mark and Strength.stronger(self.strength, self.v2.walk_strength):
               self.direction = Direction.FORWARD
           else:
               self.direction = Direction.BACKWARD
When called from Constraint, it uses the one at UrnaryConstraint. How does it know which one to use?
Thanks,
Jen
Perhaps the key point to remember is that when looking up the methods on an object, those methods are part of the object as a whole, not particually "attached" to a given class. When creating the subclass typed object, first the most base class part is
Perhaps a more modern approach would be to use the concept of an "abstract base" which allows the base to indicate that a derived class needs to define certain abstract methods, (If you need that sort of support, not defining a method might just meanthe subclass doesn't support some optional behavior defined by the base)
--
Richard Damon
--
https://mail.python.org/mailman/listinfo/python-list
The base class:
class Constraint(object):
def __init__(self, strength):
       super(Constraint, self).__init__()
       self.strength = strength
def satisfy(self, mark):
       global planner
       self.choose_method(mark)
The subclass:
class UrnaryConstraint(Constraint):
def __init__(self, v, strength):
       super(UrnaryConstraint, self).__init__(strength)
       self.my_output = v
       self.satisfied = False
       self.add_constraint()
   def choose_method(self, mark):
       if self.my_output.mark != mark and \
          Strength.stronger(self.strength, self.my_output.walk_strength):
self.satisfied = True
       else:
           self.satisfied = False
The base class Constraint doesn’t have a "choose_method" class method, but it’s called as self.choose_method(mark) on the final line of Constraint shown above.
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?
Also, this program also has a class BinaryConstraint that is also a subclass of Constraint and it also has a choose_method class method that is similar but not identical:
def choose_method(self, mark):
   if self.v1.mark == mark:
           if self.v2.mark != mark and Strength.stronger(self.strength, self.v2.walk_strength):
               self.direction = Direction.FORWARD
           else:
               self.direction = Direction.BACKWARD
When called from Constraint, it uses the one at UrnaryConstraint. How does it know which one to use?
Thanks,
Jen
On 2023-03-26 19:43:44 +0200, Jen Kris via Python-list wrote:
The base class:[...]
class Constraint(object):
def satisfy(self, mark):[...]
       global planner
       self.choose_method(mark)
The subclass:
class UrnaryConstraint(Constraint):
   def choose_method(self, mark):
       if self.my_output.mark != mark and \
          Strength.stronger(self.strength, self.my_output.walk_strength):
self.satisfied = True
       else:
           self.satisfied = False
The base class Constraint doesn’t have a "choose_method" class method,
but it’s called as self.choose_method(mark) on the final line of
Constraint shown above.Â
My question is:Â what makes "choose_method" a method of the base
class,
Nothing. choose_method isn't 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?Â
This works only if satisfy() is called on a subclass of Constraint which actually implements this method.
If you do something like
x = UrnaryConstraint()
x.satisfy(whatever)
Then x is a member of class UrnaryConstraint and will have a
choose_method() method which can be called.
Also, this program also has a class BinaryConstraint that is also a...
subclass of Constraint and it also has a choose_method class method
that is similar but not identical:
When called from Constraint, it uses the one at UrnaryConstraint. How
does it know which one to use?Â
By inspecting self. If you call x.satisfy() on an object of class UrnaryConstraint, then self.choose_method will be the choose_method from UrnaryConstraint. If you call it on an object of class BinaryConstraint,
then self.choose_method will be the choose_method from BinaryConstraint.
hp
PS: Pretty sure there's one "r" too many in UrnaryConstraint.
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
At the final line it calls "satisfy" in the Constraint class, and that
line calls choose_method in the BinaryConstraint class. Just as Peter >Holzer said, it requires a call to "satisfy."Â
My only remaining question is, did it select the choose_method in the >BinaryConstraint class instead of the choose_method in the
UrnaryConstraint class because of "super(BinaryConstraint, >self).__init__(strength)" in step 2 above?Â
On 26Mar2023 22:36, Jen Kris <jenkris@tutanota.com> wrote:BinaryConstraint, for example.
At the final line it calls "satisfy" in the Constraint class, and that line calls choose_method in the BinaryConstraint class. Just as Peter Holzer said, it requires a call to "satisfy."Â
My only remaining question is, did it select the choose_method in the BinaryConstraint class instead of the choose_method in the UrnaryConstraint class because of "super(BinaryConstraint, self).__init__(strength)" in step 2 above?Â
Basicly, no.
You've omitting the "class" lines of the class definitions, and they define the class inheritance, _not "__init__". The "__init__" method just initialises the state of the new objects (which has already been created). The:
super(BinaryConstraint,_ self).__init__(strength)
line simply calls the appropriate superclass "__init__" with the "strength" parameter to do that aspect of the initialisation.
You haven't cited the line which calls the "choose_method" method, but I'm imagining it calls "choose_method" like this:
self.choose_method(...)
That searchs for the "choose_method" method based on the method resolution order of the object "self". So if "self" was an instance of "EqualityConstraint", and I'm guessing abut its class definition, assuming this:
class EqualityConstraint(BinaryConstraint):
Then a call to "self.choose_method" would look for a "choose_method" method first in the EqualityConstraint class and then via the BinaryConstraint class. I'm also assuming UrnaryConstraint is not in that class ancestry i.e. not an ancestor of
The first method found is used.. You can look at it directly as "EqualityConstraint.__mro__".
In practice, when you define a class like:
class EqualityConstraint(BinaryConstraint):
the complete class ancestry (the addition classes from which BinaryConstraint inherits) gets flatterned into a "method resultion order" list of classes to inspect in order, and that is stored as the ".__mro__" field on the new class (EqualityConstraint)
So looking up:
self.choose_method()
looks for a "choose_method" method on the classes in "type(self).__mro__".
Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Thanks for your reply. You are correct about the class definition
lines – e.g. class EqualityConstraint(BinaryConstraint). I didn’t post >all of the code because this program is over 600 lines long. It's
DeltaBlue in the Python benchmark suite.Â
I’ve done some more work since this morning, and now I see what’s happening. But it gave rise to another question, which I’ll ask at the end.Â
The call chain starts at
   EqualityConstraint(prev, v, Strength.REQUIRED)Â
The class EqualityConstraint is a subclass of BinaryConstraint. The entire class code is:
   class EqualityConstraint(BinaryConstraint):
       def execute(self):
           self.output().value = self.input().value
Because EqualityConstraint is a subclass of BinaryConstraint, the init method of BinaryConstraint is called first.
During that initialization (I showed the call chain in my previous
message), it calls choose_method. When I inspect the code at >"self.choose_method(mark):" in PyCharm, it shows:
   <bound method BinaryConstraint.choose_method of <Trans_01_DeltaBlue.EqualityConstraint object at 0x7f7ac09c6ee0>>
As EqualityConstraint is a subclass of BinaryConstraint it has bound the choose method from BinaryConstraint, apparently during the BinaryConstraint init process, and that’s the one it uses. So that answers my original question.Â
But that brings up a new question. I can create a class instance with
x = BinaryConstraint(),
but what happens when I have a line like "EqualityConstraint(prev, v, >Strength.REQUIRED)"?
Is it because the only method of EqualityConstraint is execute(self)?
Is execute a special function like a class __init__?
On 27Mar2023 01:53, Jen Kris <jenkris@tutanota.com> wrote:
But that brings up a new question. I can create a class instance with
x = BinaryConstraint(),
That makes an instance of EqualityConstraint.
But that brings up a new question. I can create a class instance with
x = BinaryConstraint(), but what happens when I have a line like "EqualityConstraint(prev, v, Strength.REQUIRED)"?
I’m asking all these question because I have worked in a procedural style for many years, with class work limited to only simple classes, but now I’m studying classes in more depth. The three answers I have received today, including yours, havehelped a lot.
On 3/26/23 17:53, Jen Kris via Python-list wrote:helped a lot.
I’m asking all these question because I have worked in a procedural style for many years, with class work limited to only simple classes, but now I’m studying classes in more depth. The three answers I have received today, including yours, have
was worth a mention.
Classes in Python don't work quite like they do in many other languages.
You may find a lightbulb if you listen to Raymond Hettinger talk about them:
https://dailytechvideo.com/raymond-hettinger-pythons-class-development-toolkit/
I'd also advise that benchmarks often do very strange things to set up the scenario they're trying to test, a benchmark sure wouldn't be my first place to look in learning a new piece of Python - I don't know if it was the first place, but thought this
--
https://mail.python.org/mailman/listinfo/python-list
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 60:03:55 |
Calls: | 6,915 |
Calls today: | 5 |
Files: | 12,379 |
Messages: | 5,431,239 |