On Sun, Jun 23, 2013 at 7:35 AM, Adam Jiang <jiang.a...@gmail.com> wrote:
> Another question raised here is that what is the proper way to refer
> to parent class? For example,
>
> class A(object):
>         def __init__(self, arg):
>                 print "A"
>
> class B(A):
>         def __init__(self, arg):
>                 super(B, self).__init__(arg)
>
> Is this correct? As the result, whenever you wanted to refer to a
> method in parent class, super() functions has to be called. This seems
> inefficient.

Generally, use super() any time you want to refer to a class attribute
(such as a method) in a parent class that is overridden in the child
class. Also note that in Python 3, the call "super(B, self)" can be
condensed to just "super()".

If you're worried about efficiency, you can also explicitly name the
superclass in order to call the method directly, like:

        A.__init__(self, arg)

However, super() is generally considered the right way to do this, in
order to avoid repeating the parent class name, for brevity in Python
3, and because it is needed to correctly handle some cases of multiple
inheritance.

> How to refer to a field defined in parent class?

For instance attributes or attributes that haven't been overridden,
just write "self.attribute_name".
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to