Question about how a class operates.

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

Question about how a class operates.

Postby ooleyguy » Sat Apr 20, 2013 9:33 am

I've come across a exercise I found in a book in the library and the answer is confusing me. The exercise basically makes me make a class that compares once circle to another and whether they overlap, etc.

The code is:

Code: Select all
import math

class circle2d:
    def __init__(self, x = 0, y = 0, radius = 0):
        self.__x = x
        self.__y = y
        self.__radius = radius

    def getx(self):
        return self.__x

    def gety(self):
        return self.__y

    def getradius(self):
        return self.__radius

    def setx(self, x):
        self.__x = x

    def sety(self, y):
        self.__y = y

    def setradius(self, radius):
        self.__radius = radius

    def getarea(self):
        return self.__radius ** 2 * math.pi

    def getcircumference(self):
        return self.__radius * 2 * math.pi

    def containspoint(self, x, y):
        d = distance(x, y, self.__x, self.__y)
        return d <= self.__radius

    def contains(self, circle):
        d = distance(self.__x, self.__y, circle.__x, circle.__y)
        return d + circle.__radius <= circle.__radius

    def overlaps(self, circle):
        return distance(self.__x, self.__y, circle.__x, circle.__y) \
            <= self.__radius + circle.__radius

    def __contains__(self, anothercircle):
        return self.contains(anothercircle)

    def __lt__(self, secondcircle):
        return self.__cmp__(secondcircle) < 0

    def __le__(self, secondcircle):
        return self.__cmp__(secondcircle) <= 0

    def __gt__(self, secondcircle):
        return self.__cmp__(secondcircle) > 0

    def __ge__(self, secondcircle):
        return self.__cmp__(secondcircle) >= 0

    def __cmp__(self, secondcircle):
        if self.__radius > secondcircle.__radius:
            return 1
        elif self.__radius < secondcircle.__radius:
            return -1
        else:
            return 0

def distance(x1, y1, x2, y2):
    return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)


I understand it all except when it starts using circle, anothercircle, circle.__x, secondcircle, etc.

The code doesn't define a circle function or class, so where does circle.__x come from? Where is anothercircle and secondcircle defined?

Lastly, why are the comparison methods needed? if I create two objects from the class, I can't use the standard comparison operators without defining all of them?
ooleyguy
 
Posts: 6
Joined: Thu Apr 04, 2013 7:47 pm

Re: Question about how a class operates.

Postby stranac » Sat Apr 20, 2013 10:05 am

That's code from an actual python book?
If so, I suggest you burn it immediately. That code is absolutely terrible.

ooleyguy wrote:The code doesn't define a circle function or class, so where does circle.__x come from? Where is anothercircle and secondcircle defined?

Those things are supposed to be instances of the circle2d class.
You're supposed to pass those as arguments to the methods that use them.
But circle.__x and similar attributes with a double underscore shouldn't be accessible like that anyway.

ooleyguy wrote:Lastly, why are the comparison methods needed? if I create two objects from the class, I can't use the standard comparison operators without defining all of them?

Python has no way of knowing how you wish to compare custom objects unless you explain it.
But no, you don't need all of those.
Usually, you'll use functools.total_ordering() to do this.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Question about how a class operates.

Postby setrofim » Sat Apr 20, 2013 5:18 pm

stranac wrote:
ooleyguy wrote:Lastly, why are the comparison methods needed? if I create two objects from the class, I can't use the standard comparison operators without defining all of them?

Python has no way of knowing how you wish to compare custom objects unless you explain it.
But no, you don't need all of those.
Usually, you'll use functools.total_ordering() to do this.

Just to add to this. Each object in python has a unique ID (which is just an integer). If you do not implement any comparison methods, Python will compare objects by comparing their IDs:
Code: Select all
In [1]: class A(object): pass

In [2]: a = A()

In [3]: b = A()

In [4]: a == b
Out[4]: False

In [5]: a == a
Out[5]: True

In [6]: a < b
Out[6]: False

In [7]: a > b
Out[7]: True

In [8]: id(a)
Out[8]: 152781772

In [9]: id(b)
Out[9]: 152765708

Here, only the == and != comparisions would make any sense at all: the IDs are essentially random and the fact that one is greater than the other will not hold any significance in your proram. But even the equality comparisons may not do what you want. Two variables will be considered equal only if they refer to the exactly same object; in most cases, you would want different objects to be considered equal if all (or some) of their attributes have the same values. That is why you should override the comparison methods if you intent to comare objects.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Question about how a class operates.

Postby ooleyguy » Sat Apr 20, 2013 9:50 pm

Ok. Thank you. The book is Introduction to Programming Using Python, ISBN: 978-0-13-274718-9

If that book isn't giving me proper advice, can you point out another that is better? I'm willing to buy one instead of just checking it out from the library.
ooleyguy
 
Posts: 6
Joined: Thu Apr 04, 2013 7:47 pm

Re: Question about how a class operates.

Postby setrofim » Sun Apr 21, 2013 6:29 am

Learn Python the Hard Way for a practical, excercise-oriented introduction to the the language, or How to Think Like a Computer Scientist if you prefer something that reads a bit more like a traditional book. Both are free online.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Question about how a class operates.

Postby micseydel » Sun Apr 21, 2013 7:31 am

+1 on the second one, I used that and Magnus Lie Hetland's Beginning Python to get started (plus this forum helped a lot too!).

I think the online resource was better, although I wanted a physical book in high school so I had something to read on the bus and at other odd times. It was also nice that it had projects at the end that could help you learn to do bigger and better things. But if I were you'd, I'd have at the free stuff and not drop any money on anything until you've gotten everything you can out of the free stuff.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 939
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: tnknepp and 2 guests