1

For my purpose, I created a class that keeps an object.

The real length of that object is float, not integer. There can be 2 objects that differ by -- say .00001, and the shortest is the best for me.

For convenience I defined a __len__ method inside the class to be able to call len(obj).

However, python does not allow me for __len__ to return float, only integer.

I thought to return int(real_length * 10**K), for a given K.

Is there a better solution -- to work with floating point lengths of a class object?

EDIT:

In my class I have points in n-dimensional space, and I consider the distance between points, which is a real number, not an integer.

Could I make use of len function somehow ?

alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • 9
    I think in this case you should just define another function. – Kabie Jul 27 '13 at 10:34
  • `__len__` should return the length of the object (which is assumed to be a collection of some sort). If you need to rely on `__len__` for truthyness, define your own `len` method (or `length` property) and then use `__nonzero__`/`__bool__`. – Blender Jul 27 '13 at 10:36
  • I agree with @kabie... the `len` of a number makes little sense. The builtin `int` uses `bit_length ()` for instance which reads as to what exactly it means – Jon Clements Jul 27 '13 at 10:44
  • 3
    Your objects 'length' doesn't seem to refer to the same notion as `len()` -> int : _the number of items of a sequence or mapping._ Can you clarify as to what exactly length is for your object? Could it possibly be something like a score? – RussW Jul 27 '13 at 10:53
  • The number of points does not matter, it matters only the distances between them. – alinsoar Jul 27 '13 at 11:06
  • I defined __len__ and I rounded to the lower integer -- however, for more precision of computation, I would like to find another solution... and in the same time to make use of len() function, whose meaning is very intuitive in my case... – alinsoar Jul 27 '13 at 11:07
  • @alinsoar If you think the `len` function fits your case, you've completely misunderstood it. Its purpose is explicitly stated to calculate the length of a sequence (which is if course always an integer). Just write a `distance` function which takes two points and returns the distance as a float. – l4mpi Jul 27 '13 at 11:15
  • OK. I knew that. I needed the minimal distance between every 2 points in the class object -- and name that distance the length of the class instance. I understand that this is not possible via `__len__`, so I will not make use of it now... – alinsoar Jul 27 '13 at 11:20

1 Answers1

1

This adds a "string length" feature to a floating point number. len() on the object gives the length of the number as if it was a string

   class mynumber(float):
        def __len__(self):
            return len(self.__str__())
        pass    


    a=mynumber(13.7)
    b=mynumber(13.7000001)

    print len(a)
    print len(b)

Tested on python 2.7. Hope this helps

Here's a different answer based on your comment. It sets up an object that takes two coordinate pairs and then uses the haversine (Haversine Formula in Python (Bearing and Distance between two GPS points)) formula to find the distance betwixt them

from math import radians, cos, sin, asin, sqrt

class mypointpair(object):
    def __init__(self):
        self.coord=[]
        pass
    def add_coords(self,a,b):
        self.coord.append((a,b)) 
    def __len__(self):
        return self.haversine(self.coord[0][0], self.coord[0][1], self.coord[1][0], self.coord[1][1])


    def haversine(self,lon1, lat1, lon2, lat2):
        """
        Calculate the great circle distance between two points 
        on the earth (specified in decimal degrees)
        """
        # convert decimal degrees to radians 
        lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
        # haversine formula 
        dlon = lon2 - lon1 
        dlat = lat2 - lat1 
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a)) 
        km = 6367 * c
        return km 


pp1=mypointpair()
pp1.add_coords(53.32055555555556 , -1.7297222222222221 )
pp1.add_coords(53.31861111111111, -1.6997222222222223 )

print len(pp1)
Community
  • 1
  • 1
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
  • thanks -- but I have real numbers as distances -- the number of points is not important in my case -- it is important only the spatial distance between them. – alinsoar Jul 27 '13 at 11:05
  • Thanks -- I did something identical :). I wanted only to know what solutions do the others have for such case, and to know if the others have better solutions than I had. – alinsoar Jul 27 '13 at 11:22