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>Replace your method <code>get_name</code> of <code>Animal</code> with the following code</p>

<pre><code>@property
def name(self):
    return self.__name
</code></pre>

<p>Also remember to update the <code>toString</code> of <code>Dog</code> to</p>

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

<p>There're some things that I think it's worth point out if you're new to Python:</p>

<ul>
<li>Regarding getters and setters, Python's use the <code>@property</code> and <code>@property.setter</code> decorators instead of the <code>get_something</code>/<code>set_something</code> conventions in language such as Java.</li>
<li>Using <code>toString</code> is also not very Pythonic. First, method names should be in <code>snake_case</code>. Second, define a method with the signature <code>def __str__(self)</code> and return a <code>str</code>. Then you'll be able to do <code>print(rocky)</code> without having to call the <code>__str__</code> as you do for <code>toString</code>.</li>
<li>The proper way to use <code>super</code> in Python 3 is just <code>super()</code>, with no arguments passed (<a href="https://docs.python.org/3/library/functions.html#super" rel="nofollow">https://docs.python.org/3/library/functions.html#super</a>).</li>
</ul>

python python-3.x oop