Addition; in and outside class

This is the place for queries that don't fit in any of the other categories.

Addition; in and outside class

Postby Cereal » Fri May 09, 2014 9:56 am

Hi!

This is my class and its method add:

Code: Select all
class Interval(object):
    def __init__(self, min, max=None): #task 6
        if max is None:
            max = min
        self.min = min
        self.max = max
    def __repr__(self):
        return '[{}, {}]'.format(self.min, self.max)
    def __add__(self,other):
        f1, s1 = self.min, self.max
        if isinstance(other, int): #w
            f2, s2 = other, 1 #w
        else:
            f2, s2 = other.min, other.max
        return Interval(f1+f2, s1+s2)


I want to be able to count like this:
Code: Select all
print Interval(3,5)+1
print 1 + Interval(3,5)
print 1.0 + Interval(3,5)

The first one works automatically.

To make the second line work I wrote this:
Code: Select all
    def __radd__(self,other): #task 7
        f1, s1 = self.min, self.max
        if isinstance(other, int): #w
            f2, s2 = other, 1 #w
        else:
            f2, s2 = other.min, other.max
        return Interval(f1+f2, s1+s2)       


So the first and second line are fine.

But when I write
Code: Select all
print 1.0 + Interval(3,5)


I get this error:
Code: Select all
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
    execfile(filename, namespace)
  File "C:\Users\mat11CJo\Downloads\Homework 2 Task 1, 2, 3, 4, 5, 6, 7, 8 N le.py", line 161, in <module>
    print 1.0 + Interval(2,3)
  File "C:\Users\mat11CJo\Downloads\Homework 2 Task 1, 2, 3, 4, 5, 6, 7, 8 N le.py", line 34, in __radd__
    f2, s2 = other.min, other.max
AttributeError: 'float' object has no attribute 'min'
>>>


Line 160:
Code: Select all
print 1.0 + Interval(2,3)


Line 34:
Code: Select all
f2, s2 = other.min, other.max

in the __radd__ method.


Should I write float instead of int?
And I don't use f2, s2, so I realize now that they shouldn't be in __radd__.

Can someone help me please?

/ L.
Cereal
 
Posts: 15
Joined: Tue Apr 15, 2014 7:42 am

Re: Addition; in and outside class

Postby Mekire » Fri May 09, 2014 10:20 am

1.0 is not an instance of int, so your else block runs.

Code: Select all
>>> isinstance(1.0, int)
False
>>> isinstance(1.0, float)
True
>>> isinstance(1, (float, int))
True
>>> isinstance(1.0, (float, int))
True
>>>

Also possibly nicer than that:
Code: Select all
>>> from numbers import Number
>>> isinstance(1.0, Number)
True
>>> isinstance(1, Number)
True
>>>

And of course, probably more pythonic than either of these would be to not check type in the first place:
Code: Select all
try:
    f2, s2 = other.min, other.max
except AttributeError:
    f2, s2 = other, 1

-Mek
User avatar
Mekire
 
Posts: 1140
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests

cron