There is a problem that I cannot understand in the Python 3 mock exam, so please let me know.

Asked 2 years ago, Updated 2 years ago, 293 views

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

  • I don't understand the meaning of self.m() in [A]
  • Do you mean that [B] is instantiated?
  • Does
  • [C] define an empty ()?

Please let me know.

python

2022-09-30 21:53

1 Answers

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 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 .

Run this part of the code:

k=kusanagi()
w = wexal()
k.s()
w. [C] 

To get this result:

Need Speed?
I'm Saya.
Need Speed?
I'm David.

Generally, the following ideas are needed, and the answer is determined as a result.

  • One call for each of the two class instances requires the above four lines to be displayed
  • In other words, two lines are displayed in one call
  • 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] Answer
  • kusanagiThe 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.


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.