Looks like base class constructor not initialized
<p>I am a newbie in python and trying my hands in oops programming here.
I am initializing base class constructor in derived class , but when trying to print its attribute in base class it gives me error<code>object has no attribute</code></p>

<pre><code>import random
import os
import sys
class Animal:
    __name =""
    def __init__(self,name):
        self.__name = name
    def toString(self):
        return "{} is the animal name".format(self.__name)
    def get_name(self):
        return self.__name
cat = Animal("natasha")
print (cat.toString())
class Dog(Animal):
    __owner = ""
    def __init__(self,name,owner):
        self.__owner= owner
        #Animal.__init__(self,name)
        super(Dog, self).__init__(name)
    def toString(self):
        return "{} is Animal. And owner is: {}".format(self.__name,self.__owner)
rocky = Dog("rocky","Ronchi")
print (rocky.toString())
</code></pre>

<p>What am i doing wrong here ? I tried like calling <code>super.get_name()</code> also which was a getter function instead of <code>self.__name</code> but this also did not work.I am working on python3.4 </p>

<p>Variables starting with double underscores are said as private. They are mangled so they cannot be used in subclasses. Using CPython 3.5, you can confirm it simply with:</p>

<pre><code>&gt;&gt;&gt; rocky.__dict__
{'_Animal__name': 'rocky', '_Dog__owner': 'Ronchi'}
&gt;&gt;&gt;
</code></pre>

<p>When called from <code>Dog</code> class, <code>self.__name</code> searches a <code>_Dog__name</code> attribute and cannot find it.</p>

<p>There are two common ways do deal with it:</p>

<ul>
<li>do not use private variables but simply hidden ones, that is just use simple underscore prefixes. The variable will not be displayed by help, but will not be mangled and will be accessible from subclasses</li>
<li><p>use the getter from base class:</p>

<pre><code>def toString(self):
    return "{} is Animal. And owner is: {}".format(self.get_name(),self.__owner)
</code></pre>

<p>This correctly gives:</p>

<pre><code>&gt;&gt;&gt; print(rocky.toString())
rocky is Animal. And owner is: Ronchi
&gt;&gt;&gt; 
</code></pre></li>
</ul>

python python-3.x oop