Calling methods from classes
<p>I'm writing some classes:</p>

<pre><code>bean_version = "1.0"
from random import randint
console = []
print "Running Bean v%s" % bean_version
#Math Function
class math(object):
    def __init__(self, op1 = 0, op2 = 0):
        self.op1 = op1
        self.op2 = op2
    def add(self):
        return self.op1 + self.op2
    def sub(self):
        return self.op1 - self.op2
    def mul(self):
        return self.op1 * self.op2
    def div(self):
        return self.op1 / self.op2
</code></pre>

<p>What I realized I could do is:</p>

<pre><code>math.add(math(3,5))
==&gt;8
</code></pre>

<p>What I'm wondering is, is there any way to be able to do:</p>

<pre><code>math.add(3,5)
</code></pre>

<p>Python 2.7.10</p>

<p>Yes, of course. The problem currently is that you are defining your class such that the operands must be passed to the <em>constructor</em>, not the operators. Try this:</p>

<pre><code>class math(object):
    def add(self, op1=0, op2=0):
        return op1 + op2
    def sub(self, op1=0, op2=0):
        return op1 - op2
    def mul(self, op1=0, op2=0):
        return op1 * op2
    def div(self, op1=0, op2=0):
        return op1 / op2
m = math()
m.add(2, 3)
</code></pre>

<p>Given that none of the methods use <code>self</code>, they should actually be static methods. See <a href="https://docs.python.org/2/library/functions.html#staticmethod" rel="nofollow">https://docs.python.org/2/library/functions.html#staticmethod</a></p>

python python-2.7