0

I am trying to learn oops in python and I've created a class object. I am trying to import the module and use the methods that I've defined in it. I am learning from the book, Practical Programming. I've tried various things but no success. Any help shall be appreciated. Thanks in advance.

This is my code:

class mynum:
    def __init__(self, num, limit):
        self.num = num 
        self.limit = limit

    def numf(self):
        num =  int(input("enter a number"))
        limit = int(input('enter the limit'))
        total = 0
        while (num < limit):
            num = num + 9
            if num >= limit:
                break
            else:
                total = total + num
                print(num)
        print("total=",total)

And the last, error I got while trying:

Python 3.4.0 (default, Apr 11 2014, 13:05:18) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> import eight
>>> 
>>> numb = eight.mynum()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 2 required positional arguments: 'num' and 'limit'
>>> numb = eight.mynum(3,40)
>>> numb
<eight.mynum object at 0xb710412c>
>>> 
2
  • 4
    How does this differ from your expectation? Commented Apr 30, 2015 at 14:22
  • Forgive my ignorance, but I want to be able to use the numf() func, so that I can input two numbers and get the output there. I know I have made an error, but don't know where. Commented Apr 30, 2015 at 14:26

4 Answers 4

2

Your module import works, but your __init__() expects 2 params num and limit which you're not passing on the >>> numb = eight.mynum() line.

When you then pass them here >>> numb = eight.mynum(3,40) you get an object of your mynum type. So all is good

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it but its showing memory address. And I want output of the numbers that I am giving to the function. >> import eight >>> a = eight.mynum(3,40) >>> a <eight.mynum object at 0xb715436c> >>>
Well numb is an instance of your mynum object. To access the numbers use either numb.num or numb.limit respectively
1

When you import a class you need to create a class instance.

from eight import mynum
object = mynum()
object.numf(3,40)

2 Comments

This won't work. mynum() will fail without arguments. Then numf(3,40) will fail because it doesn't expect arguments.
@BradBudlong I know, I've removed positional args from init and also numf func doesn't need it , so it works that way.
1

def __init__(self, num, limit)

This is the method that is invoked when you call eight.mynum(). It expects to be given two input parameters num and limit, but you call it without any parameters.

If you examine the output of the console you will also see this:

TypeError: __init__() missing 2 required positional arguments: 'num' and 'limit'

Comments

0

It sounds like what you are looking for is to have the interactive Python respond with a representation for your object. That is done by providing a __repr__ method that tells Python how to represent your object. For example I added the following to your class definition:

def __repr__(self):
    return 'mynum({0},{1})'.format(self.num, self.limit)

Now when I run the same code I get:

>>> numb = eight.mynum(3,40)
>>> numb
mynum(3,40)

The initial issue that you had was with creating your mynum object. Since you have no default values for num and limit, you must provide values for them when you create the object, which you figured out.

The one method that you provided for your class doesn't make sense. It doesn't use the attributes of the class. Instead it reads in new values. It would only make sense to operate on the attributes of the object itself. It would also make sense to return a value instead of printing the total. Here is an example that would make more sense:

def numf(self):
    num = self.num
    total = 0
    while (num < self.limit):
        num = num + 9
        if num >= self.limit:
            break
        else:
            total = total + num
    return total

That results in:

>>> m = mynum(3,40)
>>> print(m.numf())
102

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.