I have been playing around with python as I am a beginner in it. I wrote following class Parent which I was reading from Udacity online course.
inheritance.py file
import inheritance # Why this import statement causing output two times?
class Parent():
def __init__(self, last_name, eye_color):
print("Parent Constructor Called")
self.last_name = last_name
self.eye_color = eye_color
class Child(Parent):
def __init__(self, last_name, eye_color, number_of_toys):
print("Child Constructor Called")
Parent.__init__(self, last_name, eye_color)
self.number_of_toys = number_of_toys
miley_cyrus = Child("Cyrus", "Blue", 5)
print(miley_cyrus.last_name)
print(miley_cyrus.number_of_toys)
As you can see I imported the same file in which I am currently writing class as well as printing the output. I got following output which is twice
Child Constructor Called
Parent Constructor Called
Cyrus
5
Child Constructor Called
Parent Constructor Called
Cyrus
5
But I was expecting it only once
Child Constructor Called
Parent Constructor Called
Cyrus
5
When I removed import statement I got the desired output (i.e. output only once ). My question is why python prints this 2 times even though I am printing it once when I use import of current file. What is happening behind?