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

  • SEARCH
  • Home
  • 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 719505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:39:00+00:00 2026-05-14T05:39:00+00:00

I’m just beginning to learn python. I wrote an example script to test OOP

  • 0

I’m just beginning to learn python. I wrote an example script to test OOP in python, but something very odd has happened. When I call a class method, Python is calling the function with one more parameter than given.

Here is the code:


1.  class Bar:
2.   num1,num2 = 0,0
3.   def __init__(num1,num2):
4.    num1,num2 = num1,num2
5.   def foo():
6.    if num1 > num2:
7.     print num1,'is greater than ',num2,'!'
8.    elif num1 is num2:
9.     print num1,' is equal to ',num2,'!'
10.   else:
11.    print num1,' is less than ',num2,'!'
12. a,b = 42,84
13. t = Bar(a,b)
14. t.foo
15. 
16. t.num1 = t.num1^t.num2
17. t.num2 = t.num2^t.num1
18. t.num1 = t.num1^t.num2
19. 
20. t.foo

And the error message I get:


python test.py
Traceback (most recent call last):
  File "test.py", line 13, in 
t = Bar(a,b)
TypeError: init() takes exactly 2 arguments (3 given) 

Can anyone help?
Thanks in advance

  • 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-14T05:39:00+00:00Added an answer on May 14, 2026 at 5:39 am

    Couple things:

    1. Your class is named Bar but you’re calling it as bar(a, b). Change that to Bar(a, b) to solve this problem.
    2. Classes need to inherit from something (if nothing, then object). You need class Bar(object):
    3. Instance methods, in Python, are always supplied one parameter, which is the object itself, before other passed parameters. So your def __init__(num1, num2): should be def __init__(self, num1, num2):, and the same for def foo().
    4. All of your instance variables need to be prefixed by self.. So num1 should be self.num1, etc.
    5. The ^ operator is boolean XOR. Not sure if this is what you want, it’s often confused with the power operator, **.

    Here’s your example, cleaned up and fixed accordingly:

    class Bar(object):
        num1, num2 = 0, 0
        def __init__(self, num1, num2):
            self.num1, self.num2 = num1, num2
    
        def foo(self):
            if self.num1 > self.num2:
                print self.num1,'is greater than ',self.num2,'!'
            elif self.num1 is self.num2:
                print self.num1,' is equal to ',self.num2,'!'
            else:
                print self.num1,' is less than ',self.num2,'!'
    
    a, b = 42, 84
    t = Bar(a, b)
    t.foo()
    
    t.num1 = t.num1 ^ t.num2
    t.num2 = t.num2 ^ t.num1
    t.num1 = t.num1 ^ t.num2
    
    t.foo()
    

    And the result:

    42  is less than  84 !
    84 is greater than  42 !
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 416k
  • Answers 416k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would write a program that converts the binary format… May 15, 2026 at 9:12 am
  • Editorial Team
    Editorial Team added an answer From the horse's mouth (MSDN) Why is the C# switch… May 15, 2026 at 9:11 am
  • Editorial Team
    Editorial Team added an answer There are things like Kaleido: http://www.digitalairways.com/kaleido-engine.htm which are very nice,… May 15, 2026 at 9:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.