|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do I call a method defined in a base class from a derived class that overrides it?
Answer: If you're using new-style classes, use the built-in super() function:
class Derived(Base): def meth (self): super(Derived, self).meth()
If you're using classic classes: For a class definition such as class Derived(Base): ... you can call method meth() defined in Base (or one of Base's base classes) as Base.meth(self, arguments...). Here, Base.meth is an unbound method, so you need to provide the self argument.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7094 users |
Added on |
9/23/2014 |
Views |
68712 |
Rate it! |
|
|
Question:
How do I call a method defined in a base class from a derived class that overrides it?
Answer:
If you're using new-style classes, use the built-in super() function:
class Derived(Base): def meth (self): super(Derived, self).meth()
If you're using classic classes: For a class definition such as class Derived(Base): ... you can call method meth() defined in Base (or one of Base's base classes) as Base.meth(self, arguments...). Here, Base.meth is an unbound method, so you need to provide the self argument. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
I want to do a complicated sort: can you do a Schwartzman Transform in Python?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I create a multidimensional list?
|
View Answer
|
|
How do you remove duplicates from a list?
|
View Answer
|
|
How do I iterate over a sequence in reverse order?
|
View Answer
|
|
What's a negative index?
|
View Answer
|
|
How do I convert between tuples and lists?
|
View Answer
|
|
Is there a scanf() or sscanf() equivalent?
|
View Answer
|
|
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
View Answer
|
|
How do I use strings to call functions/methods?
|
View Answer
|
|
How do I modify a string in place?
|
View Answer
|
|
How do I convert a number to a string?
|
View Answer
|
|
Is there an equivalent of C's "?:" ternary operator?
|
View Answer
|
|
How can my code discover the name of an object?
|
View Answer
|
|
Explain about pickling and unpickling?
|
View Answer
|
|
Explain about repr function?
|
View Answer
|
|
Explain about assert statement?
|
View Answer
|
|
What is a Lambda form?
|
View Answer
|
|
Explain about raising error exceptions
|
View Answer
|