I'm studying for the Python 3 basic certification exam, but I don't understand why it happens when I look at the answers in the Prime Study mock exam.I would appreciate it if you could explain.
problem
[A][B][C]Choose the appropriate choice to enter
[Results]
Need Speed?
I'm Saya.
Need Speed?
I'm David.
class Kusanagi():
defs(self):
print("Need Speed?")
[A][A]
defm(self):
print("I'm Saya.")
class wexal(kusanagi):
def [B]:
print("I'm David.")
k=kusanagi()
w = wexal
k.s()
w. [C]
Answer
[A]self.m()
[B] m(self)
[C] s( )
Question
self.m()
in [A]()
?Please let me know.
python
By the way, maybe it was a transcription error when I asked a question, the class is Kusanagi
, the others are kusanagi
, and the K
case letters are mixed.
And w=wexal
is missing the ()
at the back.
It's about inheriting classes and overriding methods.
For example, this article may be helpful.
[Introduction to Python] Class inheritance, method override and super
Then the answers are as follows:
I don't understand the meaning of self.m()
in [A]
You are invoking the m
method for your instance.
Do you mean that [B] is instantiated?
Override and define a method with the same name as the method in the parent class.
If the wexal
class instance executes the m
method, this action is taken.
Does [C] define an empty ()
?
Invoking the s
method for the wexal
class.
Since Run this part of the code: To get this result: Generally, the following ideas are needed, and the answer is determined as a result.wexal
itself does not have the s
method, execute the s
method inherited from the parent class.
When calling the s
method, the self
is an instance of the wexal
class, so if you run self.m()
within the s
method, the wexal
class runs and displays .
k=kusanagi()
w = wexal()
k.s()
w. [C]
Need Speed?
I'm Saya.
Need Speed?
I'm David.
kusanagi
class s
method requires you to call your own m
method in order for the second line I'm Saya.
to appear in the s
class [A] answer
kusanagi
You must call the s
method in order for the first line of need speed?
to appear in the wexal
class that inherited the class.→[C] Answerkusanagi
The wexal
class inherits the wexal
class requires the I'm David.
override the m
method (defining a method of the same name) to display the I'm David→Drong.
© 2025 OneMinuteCode. All rights reserved.