Defining "add" function in a class
<p>I'm writing my own code language in Python (called Bean), and I want the math functions to have the syntax:</p>

<p>print math.add(3+7)<br>
==>10</p>

<p>print math.mul(4*8)<br>
==>32</p>

<p>and so on. So far my code is:</p>

<pre><code>bean_version = "1.0"
console = []
print "Running Bean v%s" % bean_version
#Math Function
class math(object):
    def __init__(self, add, sub, mul, div):
        self.add = add
        self.sub = sub
        self.mul = mul
        self.div = div
    def add(self):
        print self.add
math = math(1,0,0,0)
print math.add()
</code></pre>

<p>But this will return an error, saying that <em>TypeError: 'int' object is not callable</em>. I can change the "add" function to a different name and it will work, but I would like to use "add" as the name.<br>
Thanks (This is Python 2.7.10 by the way)</p>

<p>An object can't have two properties with the same name. So if you have a property called <code>add</code> that holds a number, it can't also have a method called <code>add</code>, because methods are just properties that happen to hold functions. When you do:</p>

<pre><code>this.add = add
</code></pre>

<p>you're replacing the method with that number. So you need to use different names.</p>

<p>You say you want to be able to call <code>math.Add()</code>. But the method you defined is <code>add</code>. It should be:</p>

<pre><code>def Add():
    print this.add
</code></pre>

<p>This doesn't conflict with the <code>add</code> property that holds the number, since names are case-sensitive.</p>

python python-2.7