Reputation: 15
I am quite new in python, so I hope you can help me with this silly problem as I couldnt find any reason for this problem to happen. So, I have a file called calcoo.py which has a class called Calculator that sums and subtracts, and then I inherit that class in another class called CalculatorCHild (located in another py file of the same directory) that just extend the behaviour of Calculator adding the divide and multiply methods. So far it works but gives me duplicated results when suming and it is like it considers that the rest of the program calcco.py is inside the class Calculator. So here is my code:
calcoo.py file:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
operator1= sys.argv[1]
operation= sys.argv[2]
operator2= sys.argv[3]
try:
operator1 = float(sys.argv[1])
operator2 = float(sys.argv[3])
except ValueError:
sys.exit("Error: Non numerical Parameters")
class Calculator():
def sumatory(self):
return float(operator1) + float(operator2)
def substract(self):
return float(operator1) - float(operator2)
if operation == "sum":
print Calculator().sumatory()
elif operation == "substract":
print Calculator().substract()
else:
print "Error, operation not supported."
calcoochild.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
operator1= sys.argv[1]
operation= sys.argv[2]
operator2= sys.argv[3]
try:
operator1 = float(sys.argv[1])
operator2 = float(sys.argv[3])
except ValueError:
sys.exit("Error: Non numerical Parameters")
from calcoo import Calculator
class CalculatorChild(Calculator):
def multiply(self):
return float(operator1) * float(operator2)
def divide(self):
if operator2 == 0:
print "Division by zero is not allowed."
else:
return float(operator1) / float(operator2)
if operation == "sum":
print CalculatorChild().sumatory()
elif operation == "substract":
print CalculatorChild().substract()
elif operation == "multiply":
print CalculatorChild().multiply()
elif operation == "divide":
print CalculatorChild().divide()
else:
print "Error, operation not supported."
When I execute calcoo.py everything works fine, but when I exectute python calcoochild.py 3 sum 2.1 for example it prints 5.1 twice and if I write multiply it prints:
Error, operation not supported
6.3
So it is like CalculatorCHild inherit not only the methods sumatory and substract, it also exectues the if clause that it's outside of the class, I have tried to find a solution but it keeps giving me the same result. I hope someone can help me, thank you in advance.
Upvotes: 0
Views: 55
Reputation: 1122372
When you import calcoo
, all code at the top level is executed. That includes parsing sys.argv
values.
Move whatever should only be executed when running the module as a script to a block guarded by a test for the module name; if the name is __main__
then your code is run as a script, otherwise it was imported as a module:
class Calculator():
def sumatory(self):
return float(operator1) + float(operator2)
def substract(self):
return float(operator1) - float(operator2)
if __name__ == '__main__':
import sys
operator1= sys.argv[1]
operation= sys.argv[2]
operator2= sys.argv[3]
try:
operator1 = float(sys.argv[1])
operator2 = float(sys.argv[3])
except ValueError:
sys.exit("Error: Non numerical Parameters")
if operation == "sum":
print Calculator().sumatory()
elif operation == "substract":
print Calculator().substract()
else:
print "Error, operation not supported."
Now, when you import calcoo
, only the Calculator
class will be defined; the rest of the code will not be run.
Upvotes: 2