Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:10:06+00:00 2026-05-26T13:10:06+00:00

EDIT2: Thank you all for your help! EDIT: on adding @staticmethod, it works. However

  • 0

EDIT2: Thank you all for your help!
EDIT: on adding @staticmethod, it works. However I am still wondering why i am getting a type error here.

I have just started OOPS and am completely new to it. I have a very basic question regarding the different ways I can call a function from a class.
I have a testClass.py file with the code:

class MathsOperations:
    def __init__ (self, x, y):
        self.a = x
        self.b = y
    def testAddition (self):
        return (self.a + self.b)

    def testMultiplication (self):
        return (self.a * self.b)

I am calling this class from another file called main.py with the following code:

from testClass import MathsOperations

xyz = MathsOperations(2, 3)
print xyz.testAddition()

This works without any issues. However, I wanted to use the class in a much simpler way.

I have now put the following code in the testClass.py file. I have dropped the init function this time.

class MathsOperations:
    def testAddition (x, y):
        return x + y

    def testMultiplication (a, b):
        return a * b

calling this using;

from testClass import MathsOperations
xyz = MathsOperations()
print xyz.testAddition(2, 3)

this doesn’t works. Can someone explain what is happening wrongly in case 2? How do I use this class?

The error i get is “TypeError: testAddition() takes exactly 2 arguments (3 given)”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:10:07+00:00Added an answer on May 26, 2026 at 1:10 pm

    you have to use self as the first parameters of a method

    in the second case you should use

    class MathOperations:
        def testAddition (self,x, y):
            return x + y
    
        def testMultiplication (self,a, b):
            return a * b
    

    and in your code you could do the following

    tmp = MathOperations()
    print tmp.testAddition(2,3)
    

    if you use the class without instantiating a variable first

    print MathOperation.testAddtion(2,3)
    

    it gives you an error "TypeError: unbound method"

    if you want to do that you will need the @staticmethod decorator

    For example:

    class MathsOperations:
        @staticmethod
        def testAddition (x, y):
            return x + y
    
        @staticmethod
        def testMultiplication (a, b):
            return a * b
    

    then in your code you could use

    print MathsOperations.testAddition(2,3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT 2 Thank you all for your help! By fusing the answers and some
EDIT: Thank you all for your help. I edited my Database class to contain
EDIT: I have fixed all but two warnings now, so thank you all for
edit: many thanks for all the answers. Here are the results after applying the
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating
EDIT: I'm still waiting for more answers. Thanks! In SQL 2000 days, I used
Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please
What if a user hits my site with http://www.mysite.com/Quote/Edit rather than http://www.mysite.com/Quote/Edit/1000 In other
Please let me know if you have any idea about it. Thanks EDIT What
EDIT: This question is more about language engineering than C++ itself. I used C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.