Defining "add" function in a class

Ty Staszak

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)

Ralf Rafael Frix

If you don't want to change the name of the add function, you can just change self.add. These two adds 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.

edited at
0

Comments

0 comments
Login to comment

Related

Error defining function outside the class

Write a class "AntraMath" & Constructor function along with defining functions "add" , "multiply", and "done" So that the value of "res" printed is 30

Why are parenthesis optional when defining a class, but mandatory when defining a function?

Defining and Calling a Function within a Python Class

Defining a class inside a function to interrupt decorator execution

Defining custom function/property inside anonymous class

Defining a templated friend function inside a template class

Defining a function with string.replace within a class

Kotlin allows defining data class in a function, why?

Defining variadic function specialization in child class

Defining function with return value as another class in Java

Receiving an Error While Defining a Class Member Function

Defining function body outside class declaration is not compiling

Add function to a class

Creating an instance of a class without defining the __init__ function

Redefinition error when defining friend function inside class template

defining a private member function outside class definition and use of static data

Defining a Class method vs Passing object as parameter to an Function in OOP

Defining template function with std::is_invocable_v outside of class

Is it possible to write a function with "self" as an argument without defining a class?

defining function inside of function

Add class properties to regular function

Add recursive function to class as member

Function does not remove and add class

why declaring an object of a class before defining it gives error in friend class but not in friend function

Why does defining new class sometimes call the __init__() function of objects that the class inherits from?

Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

Defining a Single Template for a Member Function within a Class Template with Both Templates Used in Member Function

Defining "boolness" of a class in python