I'm writing my own code language in Python (called Bean), and I want the math functions to have the syntax:
print math.add(3+7)
==>10
print math.mul(4*8)
==>32
and so on. So far my code is:
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()
But this will return an error, saying that TypeError: 'int' object is not callable. I can change the "add" function to a different name and it will work, but I would like to use "add" as the name.
Thanks (This is Python 2.7.10 by the way)
If you don't want to change the name of the add function, you can just change self.add
. These two add
s are conflicting with each other. You will not get any error if you run this:
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.adds = add
self.sub = sub
self.mul = mul
self.div = div
def add(self):
print self.adds
math = math(1,0,0,0)
print math.add()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments