Python Mathematical signs in function parameter?
Question: I would like to know if there is a way to add math symbols into the function
parameters.

    
    
    def math(x, y, symbol):
          answer = x 'symbol' y
          return answer
    

this is an small example what I mean.

**EDIT: here is the whole problem**

    
    
    def code_message(str_val, str_val2, symbol1, symbol2):
        for char in str_val:
    
            while char.isalpha() == True:
                code = int(ord(char))
                if code < ord('Z'):
                    code symbol1= key
                    str_val2 += str(chr(code))
                elif code > ord('z'):
                    code symbol1= key
                    str_val2 += str(chr(code))
                elif code > ord('A'):
                    code symbol2= key
                    str_val2 += str(chr(code))
                elif code < ord('a'):
                    code symbol2= key
                    str_val2 += str(chr(code))
                break
            if char.isalpha() == False:
                str_val2 += char
        return str_val2
    

I need to call the function a number of times but sometimes with a +/- for
first symbol and sometimes a +/- for second symbol

ORIGINAL CODE :

    
    
    def code_message(str_val, str_val2):
        for char in str_val:
    
            while char.isalpha() == True:
                code = int(ord(char))
                if code < ord('Z'):
                    code -= key
                    str_val2 += str(chr(code))
                elif code > ord('z'):
                    code -= key
                    str_val2 += str(chr(code))
                elif code > ord('A'):
                    code += key
                    str_val2 += str(chr(code))
                elif code < ord('a'):
                    code += key
                    str_val2 += str(chr(code))
                break
            if char.isalpha() == False:
                str_val2 += char
        return str_val2
    


Answer: You can not pass operator to the function, but however you may pass operator's
functions defined in
[`operator`](https://docs.python.org/2/library/operator.html) library. Hence,
your function will be like:

    
    
    >>> from operator import eq, add, sub
    >>> def magic(left, op, right):
    ...     return op(left, right)
    ...
    

**Examples** :

    
    
    # To Add
    >>> magic(3, add, 5)
    8
    # To Subtract
    >>> magic(3, sub, 5)
    -2
    # To check equality
    >>> magic(3, eq, 3)
    True
    

**Note** : I am using function as `magic` instead of `math` because `math` is
default python library and it is not good practice to use predefined keywords.

