Reputation: 19
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>
>>>
Upvotes: 0
Views: 159
Reputation: 1785
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
Upvotes: 0
Reputation: 16081
When you import a class you need to create a class instance.
from eight import mynum
object = mynum()
object.numf(3,40)
Upvotes: 1
Reputation: 6822
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
Upvotes: 2
Reputation: 346
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'
Upvotes: 1