4

I'm very new to Python and trying to learn how classes, methods, scopes, etc works by building very silly programs with no real purpose.

The code I wrote below is suppose to just define a class Functions that is instantiated using an x and a y value and then one can execute various simple math functions like add subtract, multiply or divide (yes I know there is a Python Math library).

However, whenever I run my code and I get to the section where I want to run a math function in my class it runs the entire program over again and then does the math function.

What am I doing wrong here?

The file name is MyMath.py

class Functions():

   def __init__(self, x, y):
       self.x = x
       self.y = y

   def add(self):
      return self.x+self.y

   def subtract(self):
       return self.x-self.y

   def multiply(self):
       return self.x*self.y

   def divide(self):
       return self.x/self.y

def check_input(input):
    if input == int:
        pass
    else:
        while not input.isdigit():
            input = raw_input("\n " + input + " is not a number.  Please try again: ")

    return input

print("Welcome to the customzied Math program!")
x = raw_input("\nTo begin, please enter your first number: ")
x = check_input(x)

y = raw_input("Enter your second number: ")
y = check_input(y)

from MyMath import Functions 

math = Functions(x,y)
print(math.add())
nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • 2
    If all of the above code is in the same file, the line `from MyMath import Functions` will cause all module level code in the MyMath module to be executed once. So, if you run MyMath as a script, it will be executed twice. – user2390182 Dec 22 '15 at 15:25
  • 2
    Well, for one thing, you're not referencing the instance attributes correctly. Also, `if input == int:` doesn't do what you think it does. – Morgan Thrapp Dec 22 '15 at 15:25
  • `check_input` is wrong, but that's an issue for another question. – chepner Dec 22 '15 at 15:26
  • @MorganThrapp I just realized that myself, I need to do `self.x + self.y`, right? – nullByteMe Dec 22 '15 at 15:26

1 Answers1

11

Remove the following statement.

from MyMath import Functions

The first line of the program defines the name Functions, and you can use it without having to import it. You only use the import command if the class (or function, or variable, ...) is defined in a different file/module.

Note in addition: When you import anything from a module the whole module is run as a script (although only the Functions name is imported into the local namespace). For this reason, everything within a file to be imported should be contained inside a class or function (unless there is a good reason not to...).

Mark Perryman
  • 749
  • 7
  • 18
  • 1
    Also good to add that Python will add as a script, with the starting point being the global scope. Any code there will be executed on import as well – Zizouz212 Dec 22 '15 at 15:30
  • Any idea why this is not adding the two numbers and instead concatenating them? @Zizouz212 – nullByteMe Dec 22 '15 at 15:38
  • @Zizouz212 if I do addition from the command line it works fine, not sure what is happening with the variables... – nullByteMe Dec 22 '15 at 15:39
  • That's because you're trying to add strings. Python has different 'containers' for different types of 'data' and, you can really add those two together. From the command line, it works, because Python recognizes, and changes them to the appropriate type before they are added. – Zizouz212 Dec 22 '15 at 15:42
  • check_input() returns x and y as strings. For example `x="3"` and `y="8"`. `"Hello" + "World"` gives `"HelloWorld"`, so `"3" + "8"` gives `"38"`. In __init__(), change `self.x = x` to `self.x = int(x)`, and likewise for y. – Mark Perryman Dec 22 '15 at 15:42
  • @free_mind In your `check_input` function, change the last line from `return input` to `return int(input)`. Watch the magic :) – Zizouz212 Dec 22 '15 at 15:42
  • Thanks guys, makes sense. I'm not use to not having to define the types (coming from Java) so I forget these little things. – nullByteMe Dec 22 '15 at 15:45